[Haskell-beginners] Getting a specified number of lines from stdin
Anton Felix Lorenzen
anfelor at posteo.de
Mon Mar 7 11:29:36 UTC 2016
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
More information about the Beginners
mailing list