Higher Order Function (uncurry,curry)
I'm using these functions:
cplus a b = a+b
ucplus(a, b) = a+b
...to do this experiment:
uncurry cplus=\(a, b) -> cplus a b
(a,b)
is the parameter of the function cplus
, so the right side of equatio开发者_StackOverflow中文版n is actually doing the function cplus a b
.
Why does uncurry cplus
equal the right side of the equation?
uncurry
is defined as follows:
uncurry f = \(a,b) -> f a b
When you apply the function uncurry
to the function f
, it returns a function like f
, but instead of a 'curried' function, you get a function which takes a tuple.
The type signature of uncurry
is (a -> (b -> c)) -> ((a, b) -> c)
. That is, it takes a function of type a -> (b -> c)
and returns a function of type (a, b) -> c
.
cplus
is a curried function -- that means, it is a function that returns another function. The curried form of cplus
could be written:
cplus = (\a -> (\b -> a + b))
But in most functional languages, the syntax cplus a b = a+b
is equivalent.
精彩评论