Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Iterating a monadic action with memoization (Alex)


----------------------------------------------------------------------

Message: 1
Date: Sat, 28 Jan 2012 19:12:12 +0000 (UTC)
From: Alex <fromgmane.10.alin...@spamgourmet.com>
Subject: Re: [Haskell-beginners] Iterating a monadic action with
        memoization
To: beginners@haskell.org
Message-ID: <loom.20120128t200221-...@post.gmane.org>
Content-Type: text/plain; charset=us-ascii

Brent Yorgey <byorgey <at> seas.upenn.edu> writes:

[...]
> 
> 
> How about this?
> 
>   iterateM :: Monad m => (a -> m a) -> a -> m [a]
>   iterateM f a = (a:) `liftM` (f a >>= iterateM f)
> 
> -Brent
> 
> 
The problem with this is that it is not "lazy" in the sense that

inc :: Int -> IO Int
inc x = (print x) >> return $! x + 1

main = do
  xs <- liftM (take 5) $ iterateM inc 0
  print xs

will never terminate. It will keep printing all natural numbers but 
it will never print the list xs. I don't quite understand why this is
so, nor do I know how to rewrite iterateM to get the desired behavior.
But I wish someone would enlighten me :-) 





------------------------------

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 43, Issue 33
*****************************************

Reply via email to