[Haskell-beginners] IO question

Thomas Davie tom.davie at gmail.com
Thu Oct 27 17:08:31 CEST 2011


The issue you're hitting I believe is two fold:

1) stdin has line buffering turned on.  This means that the first example does not run until return is pressed because the input is not sent to ghci until then.
2) You now have a 3rd character on stdin that's yet to be consumed – a new line character, this causes getTwoChars to grab that, and the following character when it's run a second time.

You could solve this by turning off buffering on stdin, or by making getTwoChars ignore new lines.

Bob
if (*ra4 != 0xffc78948) { return false; }

On 27 Oct 2011, at 15:48, 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')
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20111027/78168f9d/attachment.htm>


More information about the Beginners mailing list