Hi guys,
I'm writing a list reversal program for haskell.
I've got the idea for the list reversal and that has lead to the following code:
myreverse list1
| list1 == [] = list1
| otherwise = (myreverse(tail list1)):(head list1)
Unfortunately the above code results in the following error:
Occurs check: cannot construct the infinite type: a = [[a]]
Expected type: [[a]]
Inferred type: a
In the second argument of '(:)', namely '(head list1)'
In the expression: (myreverse(tail list1)):(head list1)
PS: I get the same sort of error when I run it on a snippet that I wrote called mylast coded below:
mylast list
| list == [] = []
| otherwise = mylast_helper(head list1)(tail list1)
mylast_helper item list2
| list2 == [] = item
| otherwise = mylast_helper(head list2)(tail list2)
Error occurs at the otherwise case of the recursive helper.
EDIT: Thanks for all the input, I guess I forgot to mention that the constraints of the question forbid the use of the ++ operator. I'll keep that in mind for future questions I create.
Cheers, -Zigu