Difference between revisions of "Do notation considered harmful"

From HaskellWiki
Jump to: navigation, search
(some draft)
(didactic problems)
Line 1: Line 1:
<!--
 
 
 
== Criticism ==
 
== Criticism ==
   
Haskell's <hask>do</hask> notation is popular and ubiquitous.
+
Haskell's [[do notation]] is popular and ubiquitous.
 
However we shall not ignore that there are several problems.
 
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.
 
Here we like to shed some light on aspects you may not have thought about, so far.
   
-->
 
  +
=== Didactics ===
  +
  +
The <hask>do</hask> notation hides functional details.
  +
This is wanted in order to simplify writing imperative style code fragments.
  +
The downsides are
  +
* that, since <hask>do</hask> notation is used almost everywhere, where <hask>IO</hask> takes place, newcomers quickly believe that the <hask>do</hask> notation is necessary for doing <hask>IO</hask>,
  +
* and that newcomers think, that <hask>IO</hask> is somehow special and non-functional, in contrast to the advertisement for Haskell being purely functional.
  +
  +
These misunderstandings let people write clumsy code like
  +
<haskell>
  +
do putStrLn "text"
  +
</haskell>
  +
instead of
  +
<haskell>
  +
putStrLn "text"
  +
</haskell>
  +
or
  +
<haskell>
  +
do text <- getLine
  +
return text
  +
</haskell>
  +
instead of
  +
<haskell>
  +
getLine
  +
</haskell>
  +
or
  +
<haskell>
  +
do
  +
text <- readFile "foo"
  +
writeFile "bar" text
  +
</haskell>
  +
instead of
  +
<haskell>
  +
readFile "foo" >>= writeFile "bar"
  +
</haskell>
  +
.
  +
  +
=== Library design ===
  +
   
 
== See also ==
 
== See also ==

Revision as of 06:59, 5 November 2007

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, where IO takes place, newcomers quickly believe that the do notation is necessary for doing IO,
  • 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