[Haskell-cafe] IO, sequence, lazyness, takeWhile

Jacek Generowicz jacek.generowicz at cern.ch
Mon Dec 13 15:15:37 CET 2010


-- Is it possible to rewrite code written in this style

untilQuit = do
   text <- getLine
   report text
   if text == "quit"
      then return ()
      else untilQuit

-- in a style using higher order functions for abstract iteration? For
-- example, something along these lines:

untilQuit' = (fmap (takeWhile (/= "quit"))) (sequence $ map (>>=  
report) (repeat getLine))

-- The latter version shows the report, but it doesn't stop at the
-- appropriate place, so I'm guessing that I'm being bitten by my
-- ignorance about the interaction of actions and lazyness.


-- For completeness, here's a definition of report
report text = do
   putStrLn $ "You wrote " ++ text
   return text




More information about the Haskell-Cafe mailing list