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.<br><br><div class="gmail_quote">On Mon, Sep 13, 2010 at 3:21 PM, Daniel Fischer <span dir="ltr"><<a href="mailto:daniel.is.fischer@web.de">daniel.is.fischer@web.de</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="im">On Monday 13 September 2010 20:42:49, Alex Rozenshteyn wrote:<br>
> I'm trying to build a list where each entry depends on the previous one.<br>
> Unfoldr seemed like a good idea at the time.<br>
> Unfortunately, my values are monadic values (specifically RVars, from<br>
> the random-fu package). Okay, shouldn't be a problem; I just monadic<br>
> bind and...<br>
><br>
> > -- example code<br>
> > updateCell :: Bool -> RVar Bool<br>
> > updateCell False = return False<br>
> > updateCell True = bernoulli (0.9 :: Double)<br>
> ><br>
> > sim = sequence $ take 20 $ unfoldr (\x -> Just (x, x >>= updateCell))<br>
><br>
> (return True)<br>
<br>
</div>So you get<br>
<br>
sequence [return True<br>
, return True >>= updateCell<br>
, (return True >>= updateCell) >>= updateCell<br>
, ((return True >>= updateCell) >>= updateCell) >>= updateCell<br>
, ... ]<br>
<div class="im"><br>
><br>
> > runRVar sim DevURandom<br>
><br>
> [True,True,True,True,True,False,True,False,True,False,False,False,False,<br>
>False,False,True,True,False,False,False]<br>
><br>
> That output shouldn't be possible if I'm doing things right... It<br>
> appears that each cell has an independent history. I'm stumped.<br>
> Advice on threading monad input in general and random-fu in specific<br>
> would be appreciated.<br>
<br>
</div>What you want would be something like<br>
<br>
iterateM :: (Monad m) => (a -> m a) -> a -> m [a]<br>
iterateM act start = do<br>
next <- act start<br>
rest <- iterateM act next<br>
return (start : rest)<br>
<br>
but that would only work if the bind is sufficiently lazy, otherwise you'd<br>
have to iterate a given number of times.<br>
<br>
</blockquote></div><br><br clear="all"><br>-- <br><div dir="ltr"><div> Alex R</div></div><br>