Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert this let/where function to a Lambda in Haskell

Tags:

haskell

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)
like image 900
bevo009 Avatar asked Mar 05 '23 14:03

bevo009


1 Answers

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

like image 130
bradrn Avatar answered Mar 18 '23 15:03

bradrn