[Haskell-beginners] Randomness, lists, and unfoldr

Felipe Lessa felipe.lessa at gmail.com
Mon Sep 13 19:34:16 EDT 2010


On Mon, Sep 13, 2010 at 7:53 PM, Alex Rozenshteyn <rpglover64 at gmail.com> wrote:
> Is there a way to take a given monad's bind and wrap it to make it more
> lazy?  It doesn't seem like there should be, but I'm being hopeful.

I wouldn't bother with that.  Just write the following and be happy =).

iterateM :: (Monad m) => Int -> (a -> m a) -> a -> m [a]
iterateM 0 _ _ = return []
iterateM n act start = do
   next <- act start
   rest <- iterateM (n-1) act next
   return (start : rest)

However, I think the answer of your question is "no".  And in cases
where you can do something similar (e.g. in IO you can use
unsafeInterleaveIO), most of the time it isn't the best solution.

Cheers!

-- 
Felipe.


More information about the Beginners mailing list