[Haskell-cafe] Re: Tutorial uploaded

Philippa Cowderoy flippa at flippac.org
Wed Dec 21 13:58:14 EST 2005


On Wed, 21 Dec 2005, Peter Simons wrote:

>  > Some example for writing a text the IO oriented way:
>  >   do putStrLn "bla"
>  >      replicateM 5 (putStrLn "blub")
>  >      putStrLn "end"
>  >
>  > whereas the lazy way is
>  >   putStr (unlines (["bla"] ++ replicate 5 "blub" ++ ["end"]))
> 
> Um, maybe it's just me, but I think the first program is far
> superior to the second one. The last thing you want your I/O
> code to be is lazy. You want the exact opposite: you want it
> to be as strict as possible. Not only does the second
> version waste a lot of CPU time and memory for pointlessly
> constructing a lazily evaluated list nobody ever needs, it
> will also explode into your face the moment you use that
> approach to write any non-trivial number of bytes.
> 

Surely the actual explosion comes about because PutStr forces the lot at 
once? If PutStr were to evaluate a character at a time, the laziness would 
be slow and spew a lot of garbage to collect but not hang on to as much 
space as you suggest.

Not to say that the strict solution isn't still more efficient, of course.

-- 
flippa at flippac.org

The task of the academic is not to scale great 
intellectual mountains, but to flatten them.


More information about the Haskell-Cafe mailing list