An IO Question from a Newbie

Brandon Michael Moore brandon at its.caltech.edu
Sun Sep 14 01:23:39 EDT 2003


Hal was pretty terse, so I'll explain why switching to putStrLn will help.

stdout is line buffered.

At least by default (see hSetBuffering). That means output will only be
flushed to the screen once a newline is written. Your prompt wasn't
printed because it didn't have a newline, so it was buffered until the
second print provided one (read from the user, by way of s).

This is hardly specific to Haskell. Try this C program:

#import <stdio.h>
#import <unistd.h>
int main(int a,char** b) {
  fputs("test",stdout);
  sleep(1);
  fputs("boo!\n",stdout);
  return 0;
}

on my system this sleeps a second then prints "testboo!". I used fputs
rather than puts becuase puts appends a newline and fputs doesn't.
Similarly, putStrLn appends a newline and putStr doesn't.

This change does mean input will be entered on the next line, rather then
on the same line as the prompt. This should be acceptable. If you really
need to have a prompt at the start of the line then consider hFlush or
hSetBuffering.

Every time I've seen this question asked people post responses that only
mention hFlush, implying you need it to display output when you want it.
Please don't give newbies the impression you need hFlush for basic IO.
Monadic IO is scary enough to begin with.

This seems to be a common problem so I added some comments about it on the
Wiki, under UsingIO in the FrequentlyAskedQuestions.

Brandon

On Fri, 12 Sep 2003, Hal Daume III wrote:

> This is a buffering problem.  Use hSetBuffering to fix this (see Chapter 3
> in YAHT -- www.isi.edu/~hdaume/htut/).  Alternatively, use:
>
> > main = do putStrLn "Type Something:"
> >           ...
>
>
> in which case the "Ln" part will force it to be printed.
>
>  - Hal
>
> On Fri, 12 Sep 2003, Matt O'Connor wrote:
>
> > Hello all.  I'm new to functional programming and Haskell, but have been
> > programming in C and Java for a while.  I've been going through the tutorials
> > and whatnot on haskell.org.  I've read from the Gentle Introduction to
> > Haskell about IO and some of the other stuff and I have a question about it.
> >
> > main = do putStr "Type Something: "
> >           str <- getLine
> >           putStrLn ("You typed: " ++ str)
> >
> > When compile and run this code the "Type Something: " isn't displayed until
> > the putStrLn.  So there is no prompt.  The output looks like this.
> >
> > s
> > Type Something: You typed: s
> >
> > But if I change the putStr "Type Something: " to a putStrLn or put a \n at the
> > end of the string it displays the text immediately (ie, when I want it to).
> > Is there a good reason for this?  Am I doing something wrong?  Or do I need
> > to call some kind of standard output flush?  Thanks.
> >
> > Oh, I'm using ghc.
> >
> >
> > Matt
> >
> >
>
> --
>  Hal Daume III                                   | hdaume at isi.edu
>  "Arrest this man, he talks in maths."           | www.isi.edu/~hdaume
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>



More information about the Haskell-Cafe mailing list