Difference between revisions of "Avoiding IO"

From HaskellWiki
Jump to navigation Jump to search
(longer introduction)
(lazy construction of large data)
Line 1: Line 1:
 
Haskell requires an explicit type for operations involving input and output.
 
Haskell requires an explicit type for operations involving input and output.
 
This way it makes a problem explicit, that exists in every language:
 
This way it makes a problem explicit, that exists in every language:
Input and output functions can have so many effects, that it is hard to combine them.
+
Input and output functions can have so many effects, that the type signature says more or less that almost everything must be expected.
 
It is hard to test them, because they can in principle depend on every state of the real world.
 
It is hard to test them, because they can in principle depend on every state of the real world.
 
Thus in order to maintain modularity you should avoid IO whereever possible.
 
Thus in order to maintain modularity you should avoid IO whereever possible.
Line 9: Line 9:
 
== Lazy construction ==
 
== Lazy construction ==
   
  +
You can avoid a series of output functions
map putStr vs. putStr concat
 
  +
by constructing a complex data structure with non-IO code
  +
and output it with one output function.
  +
  +
Instead of
  +
<haskell>
  +
-- import Control.Monad (replicateM_)
  +
replicateM_ 10 (putStr "foo")
  +
</haskell>
  +
you can also create the complete string and output it with one call of <hask>putStr</hask>:
  +
<haskell>
  +
putStr (concat $ replicate 10 "foo")
  +
</haskell>
  +
  +
Similarly,
  +
<haskell>
  +
do
  +
h <- openFile "foo" WriteMode
  +
replicateM_ 10 (hPutStr h "bar")
  +
hClose h
  +
</haskell>
  +
can be shortened to
  +
<haskell>
  +
writeFile "foo" (concat $ replicate 10 "bar")
  +
</haskell>
  +
which also ensures proper closing of the handle <hask>h</hask>
  +
in case of failure.
  +
  +
Since you have now an expression for the complete result as string,
  +
you have a simple object that can be re-used in other contexts.
  +
E.g. you can also easily compute the length of the written string using <hask>length</hask<
  +
without bothering the file system, again.
   
 
== State monad ==
 
== State monad ==
Line 27: Line 58:
 
[[Category:Monad]]
 
[[Category:Monad]]
 
[[Category:Idioms]]
 
[[Category:Idioms]]
[[Category:Style]]
+
[[Category:Style]]</hask>

Revision as of 16:24, 25 December 2008

Haskell requires an explicit type for operations involving input and output. This way it makes a problem explicit, that exists in every language: Input and output functions can have so many effects, that the type signature says more or less that almost everything must be expected. It is hard to test them, because they can in principle depend on every state of the real world. Thus in order to maintain modularity you should avoid IO whereever possible. It is too tempting to get rid of IO by unsafePerformIO, but we want to present some clean techniques to avoid IO.

Lazy construction

You can avoid a series of output functions by constructing a complex data structure with non-IO code and output it with one output function.

Instead of

-- import Control.Monad (replicateM_)
replicateM_ 10 (putStr "foo")

you can also create the complete string and output it with one call of putStr:

putStr (concat $ replicate 10 "foo")

Similarly,

do
  h <- openFile "foo" WriteMode
  replicateM_ 10 (hPutStr h "bar")
  hClose h

can be shortened to

writeFile "foo" (concat $ replicate 10 "bar")

which also ensures proper closing of the handle h in case of failure.

Since you have now an expression for the complete result as string, you have a simple object that can be re-used in other contexts. E.g. you can also easily compute the length of the written string using length</hask< without bothering the file system, again. == State monad == randomIO == ST monad == STRef instead of IORef, STArray instead of IOArray == Custom type class == example getText [[Category:Monad]] [[Category:Idioms]] [[Category:Style]]