4

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)
1
  • If you need the lambda for for in-line use, then maybe (\x -> ((x+5)*(x+5))*2) 4 would result in 162. It's not elegant but is short and may work. Commented Feb 1, 2019 at 18:33

2 Answers 2

6

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 :)

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks brad! I actually tried that before, but not with the back-slashes in the following definitions. Q. In the Get Prog example, how do they get away without the backslashes?
@bevo009 I guess in Get Prog your where does not define functions (i.e., sumSqr and sqrSum are not functions). Instead, ad5, sqr, and dbl are defined as functions in the where, so they become lambdas.
@chi that makes sense, so variables/values don't need a backslash, but functions do. Cheers!.
I'll just add this for completeness, a reverse direction version of Brad's answer, using the & operator (actually what I started with) ``` import Data.Function calc x = (\ad5 sqr dbl -> (x & dbl & sqr & ad5)) (\x -> x + 5) (\x -> x * x) (\x -> x * 2) ``` ...but for some reason my markup is borked and the code is all messed up, sorry!
Just for completeness: The “exactly the same” is only true if y does not mention x, i.e. it is a non-recursive let.
|
1

The most simple and straightforward translation to a lambda would be

calc = \x -> (x*2)*(x*2) + 5

although obviously you could use basic mathematics to simplify that expression a little bit.

3 Comments

Thanks for that one-liner version :) How about the Get Prog example I showed where there are named variables? I've been stuck on that the last 2 days
I'm afraid I don't know what Get Prog is. I actually misread your final code snippet, but having now seen @bradrn's answer I agree with him, and hope this answers your question? I thought you just wanted calc translated into a lambda expression.
It's all helping me, your example also, thanks mate!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.