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
On Sun, 06 Mar 2016 11:38:47 +0100, Nicolaas du Preez <njdupreez@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 --
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
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 <njdupreez@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
Here is the answer http://stackoverflow.com/questions/34910992/iterate-io-actions-and-laziness You need to write recursive function, not to use iterage or to liftM (take 5) $ sequence $ repeat (unsafeInterleaveIO getLine) On Sun, Mar 6, 2016 at 11:38 AM, Nicolaas du Preez <njdupreez@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
That is an unnecessary use of unsafeInterleaveIO. The solution is actually quite simple: getLines n = replicateM n getLine
Note that replicateM n k = sequence (replicate n k), so this solution is equivalent to Henk-Jan van Tuyl's original solution. On Fri, Mar 11, 2016 at 1:48 PM Rein Henrichs <rein.henrichs@gmail.com> wrote:
That is an unnecessary use of unsafeInterleaveIO. The solution is actually quite simple:
getLines n = replicateM n getLine
participants (6)
-
Anton Felix Lorenzen -
frantisek kocun -
Henk-Jan van Tuyl -
KwangYul Seo -
Nicolaas du Preez -
Rein Henrichs