Lambda expression currying

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

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

Thanks, that's clearer now.
On Tue, Dec 22, 2020 at 12:55 AM 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 Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (2)
-
Francesco Ariis
-
Lawrence Bottorff