Reputation: 7728
I was reading about curried function somewhere, which sounded confusing. The example confused me even further. Lets say I have a function:
power :: (Int, Float) -> Float -- computes the nth power of b
power (n, b) =
if n == 0 then 1.0 else b * power (n-1, b)
Now I define another function powerc:: Int -> Float -> Float
such that
powerc n b =
if n == 0 then 1.0 else b * powerc (n-1) b
Can someone please explain to me how is powerc
a curried version of power
function.
Upvotes: 0
Views: 448
Reputation: 3742
The former is a function taking an Int, Float
tuple, while the latter is essentially a chain of functions each taking a single argument and returning a function taking the next.
That is, powerc
takes an Int
and returns a function that takes a Float
and returns a Float
.
You could make use of this with partial application. For example square = powerc 2
or cube = powerc 3
which are each then simple Float -> Float
functions with the value for n
captured.
Non-curried functions don't afford this easy partial application. It's nice to use partial application when the leading arguments are a kind of one-time configuration of the function's behavior. It also becomes particularly useful when trying to reshape functions to be passed to higher-order functions. For example, you could map powerc 2
(without defining) over a list to square them all.
Hope that helps!
Upvotes: 5
Reputation: 5133
Because powerc
allows partial application now:
square = powerc 2
BTW,
powerc = curry power
Upvotes: 5