laziness in IO

Amanda Clare afc@aber.ac.uk
Wed, 08 Jan 2003 18:11:38 +0000


How can I recursively collect a list of things while in the IO monad, 
and return the list lazily as it's constructed rather than waiting until 
they've all been collected?

Perhaps an example will make things clearer:


main =
   do xs <- getStrings
      putStrLn (head xs)

getStrings =
   do x <- getLine
      if x == "stop"
        then return []
        else do xs <- getStrings
                return (x:xs)


How can I make getStrings lazy? main should be able to terminate 
immediately after I've entered just one line.

Amanda

ps: This is a fake example, I'm really trying to lazily retrieve answers 
fetched from an Oracle SQL query.