Home > database >  How are let bindings represented with lambda expressions?
How are let bindings represented with lambda expressions?

Time:12-30

In a normal Haskell do block, I can specify values to be evaluated to a let binding, though I'm not quite sure how that translates to a lambda expression.

Taking the following (small) example

letEx :: IO [Char]
letEx = do
  let x = [v | v <- [1 .. 100], v `mod` 2 == 0]
  return $ show x

Would be represented with something similar to this

lambdaEx :: [Char]
lambdaEx = ([v | v <- [1 .. 100], v `mod` 2 == 0]) >>= \x -> show x

I cannot personally figure out how it properly is represented. Let expressions seem helpful, but their non do notation representation is a bit confusing.

CodePudding user response:

No, this is a let … in … expression [Haskell-wiki], so:

lambdaEx :: IO [Char]
lambdaEx = let x = [v | v <- [1 .. 100], v `mod` 2 == 0] in return (show x)

but here it makes not much sense to use a let anyway, this is just:

lambdaEx :: IO [Char]
lambdaEx = return (show [v | v <- [1 .. 100], v `mod` 2 == 0])

As you probably know already, you can remove the modulo two check in this case with:

lambdaEx :: IO [Char]
lambdaEx = return (show [2, 4 .. 100])
  • Related