How exactly do I convert this let/where function to a lambda in Haskell?
Let statement form:
calc x = let ad5 x = x + 5
sqr x = x * x
dbl x = x * 2
in
ad5 . sqr . dbl $ x
Where declaration form:
calc x = ad5 . sqr . dbl $ x
where
ad5 x = x + 5
sqr x = x * x
dbl x = x * 2
Lambda form? Maybe similar to this example from Get Prog, where the variables are declared first, and then defined at the bottom:
sumSqrOrSqrSum4 x y = (\sumSqr sqrSum ->
if sumSqr > sqrSum
then sumSqr
else sqrSum) (x^2 + y^2) ((x + y)^2)
The idea is that this let
expression:
let x = y in z
Is exactly the same as this lambda:
(\x -> z) y
Where y
is being used as a parameter and hence is bound to x
.
In your case, this would result in:
calc x = (\ad5 sqr dbl -> (ad5 . sqr . dbl $ x))
(\x -> x + 5)
(\x -> x * x)
(\x -> x * 2)
Of course, outside of this exercise few people would actually write it this way :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With