[Haskell-cafe] Re: Read a single Char

John Meacham john at repetae.net
Tue Oct 24 17:53:24 EDT 2006


On Tue, Oct 24, 2006 at 06:14:14PM +0200, Hans van Thiel wrote:
> I have a similar question. When I use getChar with Hugs the newline
> (caused by pressing 'Enter')  seems to be carried over into the next
> call of main, where it is treated as a single input character. 
> The workaround is getLine and head, but it would be nice to drop the
> newline right away. I suppose the solution offered here: 

that is the correct behavior, input is never discarded, it is just saved
up until you press enter for the first time. if you type foo<enter>,
your next four getChars will get 'f' 'o' 'o' and '\n'. it is not haskell
that is treating enter as specially, it is your terminal, getChar just
returns exactly what was passed to it.
> 
> hSetBuffering stdin NoBuffering 
> 
> will have the desired effect, but is there another way?

this will cause getChar to return right away as soon as they type the
first character. which is probably what you want. of course, if they
press an enter, you will see an enter. but you don't have to wait until
they press one.

        John

-- 
John Meacham - ⑆repetae.net⑆john⑈


More information about the Haskell-Cafe mailing list