I'm trying to understand functional programming through Haskell and I'm having so much trouble dealing with function composition.
Actually I have these two functions :
add:: Integer -> Integer -> Integer
add x y = x + y
sub:: Integer -> Integer -> Integer
sub x y = x - y
I want to be able to compose them. It doesn't make any sense okay, but it's for a learning aim.
What I've tried :
foo:: (Integer -> Integer) -> (Integer -> Integer) -> Integer
foo = add . sub
What I've understood :
Haskell uses functions with only one args, so that we return a new function to execute after each function execution.
So the first Integer
is the param type, while the second is the return type of a generated function that has to add the second number.
This will return another function (sub
) that will makes the same flow (returning a function with params etc...)
Am I right ?
Here's my actual error code :
src\Main.hs:23:7:
Couldn't match type `Integer' with `Integer -> Integer'
Expected type: Integer -> (Integer -> Integer) -> Integer
Actual type: Integer -> Integer -> Integer
In the first argument of `(.)', namely `add'
In the expression: add . sub
src\Main.hs:23:13:
Couldn't match type `Integer -> Integer' with `Integer'
Expected type: (Integer -> Integer) -> Integer
Actual type: Integer -> Integer -> Integer
Probable cause: `sub' is applied to too few arguments
In the second argument of `(.)', namely `sub'
In the expression: add . sub
I don't know what I'm doing wrong.
Can you help me understanding a bit more this error so I can find a solution ?