Read just one word from input

Hello Haskellers, Is there really no way to read input word-by-word? E.g., if user types 1 2 3 4 5 function (let's call it getWord :: IO String) should first return "1", then "2", and so on. The main idea behind that is if user entered more values then required, they won't be lost but instead consumed by subsequent reads (calls to getWord). That's how scanf() in C and 'cin >>' in C++ behave, and I'm quite surprised that I can't do that in Haskell. -- Regards, Alexander Batischev 4096R/0C8BFD03 CE6C 4307 9348 58E3 FD94 A00F 3569 61A2 0C8B FD03

Turn off buffering and call "words"
On Feb 26, 2012, at 10:05, Alexander Batischev
Hello Haskellers,
Is there really no way to read input word-by-word? E.g., if user types
1 2 3 4 5
function (let's call it getWord :: IO String) should first return "1", then "2", and so on.
The main idea behind that is if user entered more values then required, they won't be lost but instead consumed by subsequent reads (calls to getWord). That's how scanf() in C and 'cin >>' in C++ behave, and I'm quite surprised that I can't do that in Haskell.
-- Regards, Alexander Batischev
4096R/0C8BFD03 CE6C 4307 9348 58E3 FD94 A00F 3569 61A2 0C8B FD03
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Sun, Feb 26, 2012 at 13:05, Alexander Batischev
The main idea behind that is if user entered more values then required, they won't be lost but instead consumed by subsequent reads (calls to getWord). That's how scanf() in C and 'cin >>' in C++ behave, and I'm quite surprised that I can't do that in Haskell.
Almost every time I have used non-line-oriented I/O in a basic terminal I/O context, I have had to go back and rewrite it to be line-oriented. It's too easy to get yourself into odd issues where the tty line buffer and your program's notion of said buffer don't agree (if the terminal is in "icanon" mode) or discover you need backspace to work (if not). If you absolutely insist on shooting yourself in the foot this way in Haskell, it can be done; but there is no reason Haskell should make it *easy* to do so. That's C's superpower. :) -- brandon s allbery allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms

On Sun, Feb 26, 2012 at 04:58:44PM -0500, Brandon Allbery wrote:
If you absolutely insist on shooting yourself in the foot this way in Haskell, it can be done; but there is no reason Haskell should make it *easy* to do so. That's C's superpower. :)
I see. Thanks for replying! -- Regards, Alexander Batischev 4096R/0C8BFD03 CE6C 4307 9348 58E3 FD94 A00F 3569 61A2 0C8B FD03

On Sun, Feb 26, 2012 at 16:58, Brandon Allbery
On Sun, Feb 26, 2012 at 13:05, Alexander Batischev
wrote: getWord). That's how scanf() in C and 'cin >>' in C++ behave, and I'm
(polemic deleted)
Uh, belated disclaimer: I've been saying for years that scanf() and fscanf() should be banished from libc, or at minimum covered with severe "don't use this. really, DON'T USE THIS." warnings as with gets(). -- brandon s allbery allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms
participants (3)
-
Alexander Batischev
-
Brandon Allbery
-
Tim Perry