How do I use a let within a do block in ghci?

:help

 <statement>                 evaluate/run <statement>    
:{\n ..lines.. \n:}\n        multiline command

You can type :{ to start a multiline command, and type :} to end it.

So just do

 Prelude> :{
 Prelude| let a = do
 Prelude|     let b=5
 Prelude|     putStrLn $ show b
 Prelude| 
 Prelude| :} 

Be careful with layout (indentation/whitespace). Otherwise you can get parse errors in apparently correct code.

For example the following will NOT work because the indentation isn't deep enough:

Prelude> :{
Prelude| let a = do
Prelude|    let b=5
Prelude|    putStrLn $ show b
Prelude| 
Prelude| :}

It will lead to a parse error like this:

<interactive>:50:4: parse error on input ‘let’

Try this:

let a = do let { b = 5 } ; print b 

The let block can contain multiple declarations so you have to tell GHCi when they're done - that's what the brackets are for in this line.

By the way, you can use print for putStrLn . show.

Tags:

Haskell

Ghci