module Main where
rev :: [a] -> [a]
rev (x:[]) = x
rev (x:xs) = (rev xs):x
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
main = do
print (rev lst)
I'm working my way through 99 Haskell Problems and trying to write a function to reverse a list (yes, I am aware there's one already in the prelude).
My problem is that when I try to compile the code above (or just type the function definition into GHCi), I get:
Occurs check: cannot construct the infinite type: a = [a]
In the expression: rev :: [a] -> [a]
In the definition of `it': it = rev :: [a] -> [a]
I'm not quite sure where I'm going wrong with my types to get this error. I'm guessing it has something to do with how I'm pattern matching, but I don't know why.
element:listbut notlist:element(uselist ++ [element]instead). I think there is a duplicate already... - kennytmrev [] = []andrev (x:xs) = rev xs ++ [x]since this handles empty list too. - sdcvvc