[Haskell-beginners] IO question

Daniel Fischer daniel.is.fischer at googlemail.com
Thu Oct 27 17:11:39 CEST 2011


On Thursday 27 October 2011, 16:48:44, Rustom Mody wrote:
> I have this from Peyton Jones awkward squad paper
> 
> getTwoChars :: IO (Char,Char)
> getTwoChars = do
>             c1 <- getChar
>             c2 <- getChar
>             return (c1,c2)
> 
> Can someone explain what is happening here?
> *Main> getTwoChars
> ab
> ('a','b')
> *Main> getTwoChars
> a
> ('\n','a')

Hmm, here I get

Prelude> let getTwoChars :: IO (Char, Char); getTwoChars = do { x <- 
getChar; y <- getChar; return (x,y); }
Prelude> getTwoChars
ab('a','b')
Prelude> getTwoChars
ac('a','c')
Prelude>

Since your result pair appears on a different line, it seems that your 
input stream is line buffered and you have to press <Return> to make the 
input available to getChar. Then the entered newline isn't removed by the 
first getTwoChars, so it becomes the first Char gotten by the second 
invocation of getTwoChars.

What's your OS and ghc version?



More information about the Beginners mailing list