Send Beginners mailing list submissions to
[email protected]
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
[email protected]
You can reach the person managing the list at
[email protected]
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 <[email protected]>
Subject: Re: [Haskell-beginners] Iterating a monadic action with
memoization
To: [email protected]
Message-ID: <[email protected]>
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
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 43, Issue 33
*****************************************