[Haskell-beginners] Getting a specified number of lines from stdin

Henk-Jan van Tuyl hjgtuyl at chello.nl
Mon Mar 7 10:32:36 UTC 2016


On Sun, 06 Mar 2016 11:38:47 +0100, Nicolaas du Preez  
<njdupreez at yahoo.com> wrote:
:
> Why does
>
>     liftM (take 5) $ sequence $ repeat getLine
>
> not stop at reading only 5 lines from stdin but keeps on reading more?
>
> What I’m really trying to achieve is to get input from the user until
> an empty line is read with the code below.  But this doesn’t work
> as I expected, similar to the above.
>
>     liftM (takeWhile (/= "")) $ sequence $ repeat getLine
:

It seems that
   sequence $ repeat getLine
is too strict for this; to get five lines, you could use:
   sequence $ replicate 5 getLine
. To read until the first empty line, I would write something in  
do-notation.

Regards,
Henk-Jan van Tuyl


-- 
Folding at home
What if you could share your unused computer power to help find a cure? In  
just 5 minutes you can join the world's biggest networked computer and get  
us closer sooner. Watch the video.
http://folding.stanford.edu/


http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
Haskell programming
--


More information about the Beginners mailing list