
On Monday 21 Jul 2003 7:21 pm, Alastair Reid wrote:
If I try to run the program (compiled using GHC 6), it calculates all members of the list and then prints the whole list in the end. Since Haskell is 'lazy' I was expecting behaviour similar to HUGS where it prints the numbers as it finds them. Does this behaviour have something to do with the monadic IO in Haskell? I am a Haskell newbie and can't even claim having understood the "gentle introduction" properly.
My guess would be that you're seeing buffering behaviour where GHC fills up a buffer of characters for output and flushes it every 1000, 4000 or whatever characters.
IIRC correctly, buffering is off by default in Hugs but on by default in GHC.
You can turn this off with the Haskell 98 function hSetBuffering described in section 11.4.2 of the library report.
Thanks for the pointer! Ok so I added the "hSetBuffering stdout NoBuffering" line to my main function and still got the same 'nonlazy' behaviour, or so I thought. It took me some time to figure out what was going on. I am using emacs to write my code and I was calling a.out from inside it. Obviously it was emacs that was holding back the output until the program terminated! Now I feel kinda stupid :-( Anyway since I am also lazy enough to dislike switching back to the shell whenever I want to test my programs, here is another question - how do I make emacs give me the output AS AND WHEN it recieves it? If you are planning to say "Go read the manual" please also add some pointers to which part of the manual 'cos by the size of it, I'll be looking for *some* time. :-) - AJ