I am new to Haskell and cannot seem to find a reason for not being able to compile this:
test = foldr (\x y -> y : x) [1]
I am not trying to achieve anything functionally for the dummy function “test”.
Just that I keep getting this error code:
Occurs check: cannot construct the infinite type: a0 = [a0]
In the first argument of `(:)', namely `y'
In the expression: y : x
In the first argument of `foldr', namely `(\ x y -> y : x)'
All I want to do is to be able to concatenate elements from a list, to form another list within an anonymous function defined in another function (in this case, defined in “test”.)
Thanks.
The type of
foldrisso if we try to use it
we must have the following types:
since the argument for an empty input list has that type, and
But the lambda is
flip (:)and thus has the typeand trying to unify that with
a -> [n] -> [n], we findwhich implies
t == [t].If you don’t
flipthe cons(:), it will type-check, but the function would be easier expressed asor, point-free