[Haskell-cafe] unification would give infinite type

Mattias Bengtsson moonlite at dtek.chalmers.se
Tue Dec 4 08:59:59 EST 2007


Rafael skrev:
> Hi... I give this error using hugs for the code:
>
> ---------------------------------------------------
> f = foldl (\x y -> add x y) 0 [1,2,3]
> add x y = return (x + y)
> ---------------------------------------------------
> I try:
>
> f = foldl (\x y -> counter x y) (return 0) [1,2,3]
>
> but it dont solve,  and with ghci:
>
> "
>     Occurs check: cannot construct the infinite type: b = m b
>       Expected type: b
>       Inferred type: m b
>     In the expression: add x y
>     In a lambda abstraction: \ x y -> add x y
>   
return isn't what you would expect it is if you come from an imperative 
programming background.
It might be a bit early in your haskell journey for this but return is 
one of the two most important functions in the Monad typeclass and not a 
language construct as in C and Java.
Anyhow, try this definition of "add" instead:
 > add x y = x + y
or for short:
 > add = (+)

Mattias


More information about the Haskell-Cafe mailing list