Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Read just one word from input (Alexander Batischev)
   2. Re:  Read just one word from input (Tim Perry)
   3. Re:  Read just one word from input (Alexander Batischev)
   4. Re:  Read just one word from input (Brandon Allbery)
   5. Re:  Read just one word from input (Alexander Batischev)
   6. Re:  Read just one word from input (Brandon Allbery)
   7. Re:  Read just one word from input (Mike Meyer)


----------------------------------------------------------------------

Message: 1
Date: Sun, 26 Feb 2012 20:05:09 +0200
From: Alexander Batischev <[email protected]>
Subject: [Haskell-beginners] Read just one word from input
To: [email protected]
Message-ID: <20120226180509.GA16489@antaeus>
Content-Type: text/plain; charset="us-ascii"

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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20120226/b0f7551e/attachment-0001.pgp>

------------------------------

Message: 2
Date: Sun, 26 Feb 2012 12:31:06 -0800
From: Tim Perry <[email protected]>
Subject: Re: [Haskell-beginners] Read just one word from input
To: Alexander Batischev <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Turn off buffering and call "words"

On Feb 26, 2012, at 10:05, Alexander Batischev <[email protected]> wrote:

> 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
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners



------------------------------

Message: 3
Date: Sun, 26 Feb 2012 23:00:42 +0200
From: Alexander Batischev <[email protected]>
Subject: Re: [Haskell-beginners] Read just one word from input
To: [email protected]
Message-ID: <20120226210042.GB24193@antaeus>
Content-Type: text/plain; charset="us-ascii"

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.

P.S. Excuse me for mistakenly replying directly to you, Tim.

-- 
Regards,
Alexander Batischev

4096R/0C8BFD03
CE6C 4307 9348 58E3 FD94  A00F 3569 61A2 0C8B FD03

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20120226/0a94df48/attachment-0001.pgp>

------------------------------

Message: 4
Date: Sun, 26 Feb 2012 16:58:44 -0500
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] Read just one word from input
To: Alexander Batischev <[email protected]>
Cc: [email protected]
Message-ID:
        <CAKFCL4Vactzz=tdej1o2ckomr3ea9uxownibtmmwhti76j5...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Sun, Feb 26, 2012 at 13:05, Alexander Batischev <[email protected]>wrote:

> 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                                      [email protected]
wandering unix systems administrator (available)     (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20120226/92b81cc2/attachment-0001.htm>

------------------------------

Message: 5
Date: Mon, 27 Feb 2012 00:13:20 +0200
From: Alexander Batischev <[email protected]>
Subject: Re: [Haskell-beginners] Read just one word from input
To: [email protected]
Message-ID: <20120226221320.GB28175@antaeus>
Content-Type: text/plain; charset="us-ascii"

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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20120227/e706d30f/attachment-0001.pgp>

------------------------------

Message: 6
Date: Sun, 26 Feb 2012 17:33:19 -0500
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] Read just one word from input
To: Haskell Beginners <[email protected]>
Message-ID:
        <CAKFCL4XTcSO0FAUg_jBkd79V9L1zWTjmGa4c6jCb=8d8sy9...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Sun, Feb 26, 2012 at 16:58, Brandon Allbery <[email protected]> wrote:

> On Sun, Feb 26, 2012 at 13:05, Alexander Batischev <[email protected]>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                                      [email protected]
wandering unix systems administrator (available)     (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20120226/f711e3aa/attachment-0001.htm>

------------------------------

Message: 7
Date: Sun, 26 Feb 2012 17:45:23 -0500
From: Mike Meyer <[email protected]>
Subject: Re: [Haskell-beginners] Read just one word from input
To: Alexander Batischev <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

On Sun, 26 Feb 2012 23:00:42 +0200
Alexander Batischev <[email protected]> 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 <[email protected]>             http://www.mired.org/
Independent Software developer/SCM consultant, email for more information.

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



------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 44, Issue 26
*****************************************

Reply via email to