[Haskell-beginners] Read just one word from input

Mike Meyer mwm at mired.org
Sun Feb 26 23:45:23 CET 2012


On Sun, 26 Feb 2012 23:00:42 +0200
Alexander Batischev <eual.jp at gmail.com> wrote:
> On Sun, Feb 26, 2012 at 12:31:06PM -0800, Tim Perry wrote:
> > Turn off buffering and call "words"
> What "words" do you mean? There's no suck function in System.IO.
> And if you're talking about Data.List's "words", meaning something like:
>   getLine >>= words
> then it's not what I need - such a construction would consume whole
> line, while function I'm seeking should consume as few words as
> required.

Depends on what you mean by "consume". Yes, it reads the entire line
into an input buffer (unless you muck with the buffering, which as has
been indicated is a bad idea). The "words" function will then read
that buffer, parsing it into a list of words. However, since it's all
lazy, getting a single word from the list will only take a single word
from the buffer, leaving the rest there to be parsed later if you ever
want them.  This is pretty much exactly what C's scanf function does:
fills a buffer and parses as many objects out as the call requires.

C is imperative, and the I/O library is built around a globally shared
buffer for each file. Having functions that manipulate that buffer to
parse things out of it is a reasonable solution in that context.

Haskell is functional, so the I/O library is built around providing
lists of characters that can be processed with Haskell's very powerful
list processing facilities (or parser building tools, or ...).

If you're trying to write a function that calls the non-existent scanf
to get the next word, using recursion to create a loop, then add an
extra parameter to the function: a list of unused words. For the first
call, pass it the list from getLine >>= words (or maybe getContents,
depending). Use the list primitives to get the words you need, and
then pass the unused part of the list in the recursive call to process
the rest of the list. Since it's all lazy, it should have the performance characteristics you want.

      <mike
-- 
Mike Meyer <mwm at mired.org>		http://www.mired.org/
Independent Software developer/SCM consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org



More information about the Beginners mailing list