i am studying Haskell and I do not understand why the following two implementation are not equivalent. Rather why I get the error message if I try to load it in REPL.
addStuff :: Integer -> Integer -> Integer
addStuff x y = x + y + 5
addStuff_ :: Integer -> Integer -> Integer
addStuff_ x = x + (\i -> i + 5)
The erro I get is the following
• Couldn't match expected type ‘Integer’
with actual type ‘a0 -> a0’
• The lambda expression ‘\ i -> i + 5’ has one argument,
but its type ‘Integer’ has none
In the second argument of ‘(+)’, namely ‘(\ i -> i + 5)’
In the expression: x + (\ i -> i + 5)
The second function is trying to sum Integer
with another function. What you are looking for is function composition (using .
operator):
addStuff_ :: Integer -> Integer -> Integer
addStuff_ x = (x +) . (+ 5)
Or just:
addStuff' :: Integer -> Integer -> Integer
addStuff' = (+) . (+ 5)