[Haskell-beginners] Randomness, lists, and unfoldr

Alex Rozenshteyn rpglover64 at gmail.com
Mon Sep 13 19:53:45 EDT 2010


The reason I ask is that I plan on making a simulation, which will run until
the user decides to pause.

Potentially, I could keep state and update it, but that doesn't seem
haskelly.  With an infinite list, I even get history for free.

On Mon, Sep 13, 2010 at 7:34 PM, Felipe Lessa <felipe.lessa at gmail.com>wrote:

> 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.
>



-- 
          Alex R
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100913/7d0426cf/attachment.html


More information about the Beginners mailing list