unification would give infinite type

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 " thnks. att Rafael

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

Hi, Depending on what you want, you should either remove 'return' or change to 'foldM' (from Control.Monad). If you choose the latter, you also need to add a type signature to f (because of the monomorphism restriction). / Emil On 2007-12-04 14:43, Rafael wrote:
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 "
thnks.
att Rafael _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi Emil, I'm beginning in monad area...
I don't know about monomorphis restriction, but foldM works, a lot of thanks...
Matias.... tnks too, i'm conscious about "return" in the monadic chain.
thnks.
On Dec 4, 2007 12:00 PM, Emil Axelsson
Hi,
Depending on what you want, you should either remove 'return' or change to 'foldM' (from Control.Monad). If you choose the latter, you also need to add a type signature to f (because of the monomorphism restriction).
/ Emil
On 2007-12-04 14:43, Rafael wrote:
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 "
thnks.
att Rafael _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

You usually don't need to worry about it. Just keep in mind that if you happen to get a strange type error concerning an (overloaded) function *without type signature*, it sometimes helps to add a signature. / Emil On 2007-12-04 15:52, Rafael wrote:
I don't know about monomorphis restriction
participants (3)
-
Emil Axelsson
-
Mattias Bengtsson
-
Rafael