vendredi 7 août 2020

I have a problem calling a function in Haskell

This is a code the creates a list and after that it makes a random shuffle. I am getting problems with the shuffle function and data type errors. I can't even execute the program. How can it be fixed?

    import System.IO
    import System.Random
    shuffle :: [a] -> [a]
    shuffle [a] = if length [a] < 2 then return [a] else do
--                                       ^^^^^^^^^^  problem here: 
-- Occurs check:cannot construct the infinite type: a ~ [a]. 
-- Expected type: [a]. 
-- Actual type: [[a]]

       i <- randomRIO (0, length [a]-1)
       r <- shuffle (take i [a] ++ drop (i+1) [a])
       return ([a]!!i : r)
       
    main = do  -- the problem is in this line
       putStrLn "Enter the number:"  
       number <- getLine  
       let n = (read number :: Int)
       let list = [1..n]
       print list
       shuffle list
--     ^^^^^^^^^^^^ Error here: 
-- Couldn't match type ‘[]’ with ‘IO’. 
-- Expected type: IO Int. 
-- Actual type: [Int]



Aucun commentaire:

Enregistrer un commentaire