<div dir="ltr"><div>Hi,</div><div><br></div><div>Your code does not work because sequence function can't be lazy due to the strictness of IO monad's >>= operation.</div><div><br></div><div>Here's a more verbose version:</div><div><br></div><div>readWhile :: (String -> Bool) -> IO [String]<br></div><div>readWhile p = do</div><div> line <- getLine</div><div> if p line</div><div> then do</div><div> lines <- readWhile p</div><div> return $ line : lines</div><div> else</div><div> return []</div><div><br></div><div><div>ghci> readWhile (/="")<br></div><div>foo</div><div>bar</div><div><br></div><div>["foo","bar"]</div></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Mar 6, 2016 at 7:38 PM, Nicolaas du Preez <span dir="ltr"><<a href="mailto:njdupreez@yahoo.com" target="_blank">njdupreez@yahoo.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Good day All,<div><br></div><div>Why does</div><div><br></div><div><span style="font-family:Menlo;font-size:11px"> liftM (take 5) $ sequence $ repeat getLine</span></div><div><br></div><div>not stop at reading only 5 lines from stdin but keeps on reading more?</div><div><br></div><div>What I’m really trying to achieve is to get input from the user until</div><div>an empty line is read with the code below. But this doesn’t work </div><div>as I expected, similar to the above.</div><div><br></div><div><span style="font-size:11px;font-family:Menlo"> liftM (takeWhile (/= "")) $ sequence $ repeat getLine</span></div><div><span style="font-family:Menlo;font-size:11px"><br></span></div><div><br></div><div>Regards,<div>Nicolaas du Preez</div></div></div><br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>