[Haskell] lazy printing

Keith Wansbrough Keith.Wansbrough at cl.cam.ac.uk
Fri Sep 10 06:25:36 EDT 2004


> Dear Haskellers,
> 
> Can you tell me how to force Haskell to output the results in a
> `lazy' way? 
[..]
>     main = putStr (concat ["\n min1 = ", show min1,
>                            "\n min2 = ", show min2,
>                            "\n"
>                           ]
>                   )
[..]
> This prints       min1 = 1
> 
> and hangs silently -- probably, because  min2  takes long to compute.
> But why does not it print immediately
>                   
>                   min1 = 1
>                   min2 = 

Haskell is outputting lazily, but by default stdout is set to LineBuffering - for efficiency, a line is only written to stdout once it is complete.  Try adding "\n" to the end of the "min2 =" line to see what I mean.

To get the behaviour you describe, add import IO  and hSetBuffering stdout NoBuffering >>  to the start of your main function.

--KW 8-)
-- 
Keith Wansbrough <kw217 at cl.cam.ac.uk>
http://www.cl.cam.ac.uk/users/kw217/
University of Cambridge Computer Laboratory.



More information about the Haskell mailing list