Lambda expression currying

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Lambda expression currying

Lawrence Bottorff-2
Here's something from Learn You... 

Lambdas are normally surrounded by parentheses unless we mean for them to extend all the way to the right. Here's something interesting: due to the way functions are curried by default, these two are equivalent:

addThree :: (Num a) => a -> a -> a -> a  
addThree x y z = x + y + z  

addThree :: (Num a) => a -> a -> a -> a  
addThree = \x -> \y -> \z -> x + y + z  

If we define a function like this, it's obvious why the type declaration is what it is. There are three ->'s in both the type declaration and the equation. But of course, the first way to write functions is far more readable, the second one is pretty much a gimmick to illustrate currying.

So with the lambda version how exactly is the currying taking place? I understand something like this

doubleDouble x = (\x -> x*2) (2 * x)

So with beta reduction we have (2*x)*2, then plug in the argument.

And with this

overwrite x = (\x -> (\x -> (\x -> x) 4) 3) 2

which gives (\x -> (\x -> 4) 3) 2
(\x -> 4) 2
4

But how is the beta reduction happening with addThree?

BTW, I flunked lambda calculus in Kindergarten.


LB


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Lambda expression currying

Francesco Ariis
Hello Lawrence,

Il 21 dicembre 2020 alle 23:59 Lawrence Bottorff ha scritto:
> addThree :: (Num a) => a -> a -> a -> a
> addThree = \x -> \y -> \z -> x + y + z
> […]
>
> But how is the beta reduction happening with addThree?

Should be:

addThree = (\x -> \y -> \z -> x + y + z) 1 2 3
         = (\y -> \z -> 1 + y + z) 2 3              beta
         = (\z -> 1 + 2 + z) 3                      beta
         = 1 + 2 + 3                                beta
         = …

Does that make sense?
—F
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Lambda expression currying

Lawrence Bottorff-2
Thanks, that's clearer now.

On Tue, Dec 22, 2020 at 12:55 AM Francesco Ariis <[hidden email]> wrote:
Hello Lawrence,

Il 21 dicembre 2020 alle 23:59 Lawrence Bottorff ha scritto:
> addThree :: (Num a) => a -> a -> a -> a
> addThree = \x -> \y -> \z -> x + y + z
> […]
>
> But how is the beta reduction happening with addThree?

Should be:

addThree = (\x -> \y -> \z -> x + y + z) 1 2 3
         = (\y -> \z -> 1 + y + z) 2 3              beta
         = (\z -> 1 + 2 + z) 3                      beta
         = 1 + 2 + 3                                beta
         = …

Does that make sense?
—F
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners