Reputation: 13517
I am learning Haskell. I got to know that any function in Haskell can take only one argument. So, if you see a function max 2 4
; it actually is (max 2) 4
. What they say is that 1st 2
is applied (as a parameter) to max
which returns a functions, that takes 4
as the parameter. What I fail to understand is what happens when 2 is applied to max? What does it mean that it returns a function called (max 2)
?
Let me give another example, to make my question more clear. Take this function: multiply x y x = x*y*z
. They say it actually is evaluated this way: ((multiply x) y) z
. Now I give this input: multiply 2*4*5
How is this evaluated?
multiply 2
returns (multiply 2)
and 4 is applied as parameter:
(multiply 2) 4
Now what does this return -- ((multiply 2) 4)
or multiply 8
? If it multiplies 4 and 2 at this step, how does Haskell know that it has to do that (because the function can multiply only 3 parameters)?
Upvotes: 1
Views: 235
Reputation: 818
Just think it mathematically: suppose there is a function taking two variables: f(x, y)
. Fix x=2
would give you a new function with one variable: g(y)=f(2, y)
If f(x, y) = max(x, y)
which gives the maximum of x and y, g(y) = f(2, y) = max(2, y)
gives the maximum of 2 and y.
For f(x, y, z) = x * y * z
, g(y, z) = f(2, y, z) = 2 * y * z
, and h(z) = g(4, z) = f(2, 4, z) = 2 * 4 * z
.
Also you can fix x=2 and z=4 to form p(y) = f(2, y, 4)
. In Haskell it is
\y -> multiply 2 y 4
For the implementation, Haskell would not actually multiply 2 and 4 because it's lazy evaluated. That is, it would not compute a value until it has to.
Upvotes: 2