The do notation
The so-called do
notation is syntactic sugar that unclutters the use of I/O and makes Haskell code somewhat resemble that of imperative languages.
Imperative Style
The do
notation mimics the typical imperative programming style of writing sequential statements on consecutive lines. For example, here is the earlier greeting example, now with the do
notation:
main :: IO () main = do putStrLn "What is your name?" name <- getLine putStrLn ("Hello, " ++ name ++ "!")
This program contains one do
block. The block is started by the do
keyword and features one I/O action per line. The result of the do
block is the result of the last I/O action.
A do
block can be systematically desugared as follows:
- When the block consists of multiple lines, we can extract the first line and recursively desugar the remaining...