I'm trying to figure out the cause and effect logics behind "Currying" and comes to the following results:
I think the above logic 1 -> 2 -> 3 is confusing to answer "why using currying". For statement 3, one can also combine multiple tuples or lists as single argument to implementing multiple arguments function.
It seems statement 1 is not correct reason for statement 2. What I'm sure is 2->3 is correct reasoning, but what's the reason behind 2? Why Haskell, as a functional language, only take one argument?
Most books take statement 2 as an accepted fact. Does anybody know the reason behind 2? Or does anybody know the correct logic behind "why currying"?
The decision to use currying as the preferred method of multi-argument functions was made for two reasons. Firstly, Haskell has its roots firmly in lambda calculus, which uses currying. Secondly, currying allows for easy partial application, a language facet which is indescribably useful.
The function
f x y = x + y
has the type (assuming x
& y
are Integer
s)
f :: Integer -> Integer -> Integer
this is the same as
g :: Integer -> (Integer -> Integer)
Which means f
is a function that takes an Integer
and returns a new function that also takes an Integer
and returns an Integer
. Currying is the process whereby a function that takes multiple arguments is actually evaluated as a series of functions that take one argument.
Currying makes it very easy to partially apply functions as follows
plusTwo :: Integer -> Integer
plusTwo = f 2
The above code simply applies 2
to f
and binds the name plusTwo
to the resulting function.
This makes it extremely easy for developers to abstract common behavior into a single place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With