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

Gregory Crosswhite gcross at phys.washington.edu
Mon Dec 13 17:06:40 CET 2010


  Take a look at the monad-loops package.

Cheers,
Greg

On 12/13/2010 06:15 AM, Jacek Generowicz wrote:
> -- 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
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe




More information about the Haskell-Cafe mailing list