Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Getting a specified number of lines from stdin (Nicolaas du Preez) 2. Re: Getting a specified number of lines from stdin (Henk-Jan van Tuyl) 3. Re: Getting a specified number of lines from stdin (Anton Felix Lorenzen) 4. Re: Getting a specified number of lines from stdin (KwangYul Seo) ---------------------------------------------------------------------- Message: 1 Date: Sun, 6 Mar 2016 12:38:47 +0200 From: Nicolaas du Preez <njdupr...@yahoo.com> To: beginners@haskell.org Subject: [Haskell-beginners] Getting a specified number of lines from stdin Message-ID: <6b6f0c1d-7cea-46e4-9e1b-c3c78709b...@yahoo.com> Content-Type: text/plain; charset="windows-1252" Good day All, 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 Regards, Nicolaas du Preez -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160306/056cc31c/attachment-0001.html> ------------------------------ Message: 2 Date: Mon, 07 Mar 2016 11:32:36 +0100 From: "Henk-Jan van Tuyl" <hjgt...@chello.nl> To: beginners@haskell.org, "Nicolaas du Preez" <njdupr...@yahoo.com> Subject: Re: [Haskell-beginners] Getting a specified number of lines from stdin Message-ID: <op.ydx98rlopz0j5l@alquantor> Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes On Sun, 06 Mar 2016 11:38:47 +0100, Nicolaas du Preez <njdupr...@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@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 -- ------------------------------ Message: 3 Date: Mon, 7 Mar 2016 12:29:36 +0100 From: Anton Felix Lorenzen <anfe...@posteo.de> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Getting a specified number of lines from stdin Message-ID: <56dd6620.2010...@posteo.de> Content-Type: text/plain; charset=windows-1252; format=flowed You can define a mixture of takeWhile and sequence as: takeWhileM :: (a -> Bool) -> [IO a] -> IO [a] takeWhileM _ [] = return [] takeWhileM p (x:xs) = do e <- x if p e then do xs' <- takeWhileM p xs return (e : xs') else return [] and then write: ghci> takeWhileM (/="") (repeat getLine) >>= print asdf asdf ["asdf","asdf"] Yours, Anton Felix Lorenzen ------------------------------ Message: 4 Date: Mon, 7 Mar 2016 21:03:12 +0900 From: KwangYul Seo <kwangyul....@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Getting a specified number of lines from stdin Message-ID: <caclrxywnwo6p3q7_4el7uqyncftibhtwlgv-phz2trt7j22...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Hi, Your code does not work because sequence function can't be lazy due to the strictness of IO monad's >>= operation. Here's a more verbose version: readWhile :: (String -> Bool) -> IO [String] readWhile p = do line <- getLine if p line then do lines <- readWhile p return $ line : lines else return [] ghci> readWhile (/="") foo bar ["foo","bar"] On Sun, Mar 6, 2016 at 7:38 PM, Nicolaas du Preez <njdupr...@yahoo.com> wrote: > Good day All, > > 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 > > > Regards, > Nicolaas du Preez > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160307/7183cab3/attachment-0001.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 93, Issue 6 ****************************************