[Haskell] reading from stdin

Axel Simon A.Simon at kent.ac.uk
Thu Oct 4 12:46:09 EDT 2007


I'm trying to continuously output data to a file handle while reading
single characters from the user to adjust the speed at which things are
output. I'm interested to get this to work in Hugs on Windows.
I successfully used the following function ghci under Mac OS:

getUserInput :: IO (Maybe Char)
getUserInput = do
  hasInput <- hReady stdin
  if hasInput then liftM Just (hGetChar stdin) else return Nothing

This function returns a character to me if there's one available.
If anybody could give me a hint how to get this working in Hugs under
Windows, please tell me.

For the entertainment value, here's what's happening currently:

a) ghci under Unix
A character is returned if it is available, otherwise the function
returns immediately.

b) Hugs under Unix
Characters are not recognised when typed. The characters are returned as
soon as return is hit. The function does not block.

c) ghci under Windows
The function does not block until a character is entered. Once a
character is entered, the function blocks until return is hit.

d) Hugs under Windows
The function aborts immediately somewhere in IO.hWaitForInput with an
'invalid operation (unsupported opperation)' error.

I don't know what the right behaviour is, and I don't really care. But:

a) is the behaviour I want, but unfortunately for platform d)
b) must be due to ghci and Hugs having different opinions on whether
stdin should be line buffered or unbuffered
c) this is weird
d) this is broken

How can I get platform d) working with the behaviour of a)?

Thanks a lot,

P.S.: Sorry to post to haskell at ..., but nothing else seemed to match.

More information about the Haskell mailing list