Do notation considered harmful
Criticism
Haskell's do notation is popular and ubiquitous. However we shall not ignore that there are several problems. Here we like to shed some light on aspects you may not have thought about, so far.
Didactics
The do
notation hides functional details.
This is wanted in order to simplify writing imperative style code fragments.
The downsides are
- that, since
do
notation is used almost everywhere, whereIO
takes place, newcomers quickly believe that thedo
notation is necessary for doingIO
, - and that newcomers think, that
IO
is somehow special and non-functional, in contrast to the advertisement for Haskell being purely functional.
These misunderstandings let people write clumsy code like
do putStrLn "text"
instead of
putStrLn "text"
or
do text <- getLine
return text
instead of
getLine
or
do
text <- readFile "foo"
writeFile "bar" text
instead of
readFile "foo" >>= writeFile "bar"
.
Library design
See also
- Paul Hudak in Haskell-Cafe: A regressive view of support for imperative programming in Haskell
- Data.Syntaxfree on Wordpress: Do-notation considered harmful