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 (Chadda? Fouch?)


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

Message: 1
Date: Sun, 29 Jan 2012 13:04:05 +0100
From: Chadda? Fouch? <chaddai.fou...@gmail.com>
Subject: Re: [Haskell-beginners] Iterating a monadic action with
        memoization
To: Alex <fromgmane.10.alin...@spamgourmet.com>
Cc: beginners@haskell.org
Message-ID:
        <CANfjZRYYUAyi2pv9Ck5eoHogcyq5EGkG=d_nfyxo6el+ra_...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Sat, Jan 28, 2012 at 8:12 PM, Alex
<fromgmane.10.alin...@spamgourmet.com> wrote:
> Brent Yorgey <byorgey <at> seas.upenn.edu> writes:
>> ? iterateM :: Monad m => (a -> m a) -> a -> m [a]
>> ? iterateM f a = (a:) `liftM` (f a >>= iterateM f)
> The problem with this is that it is not "lazy" in the sense that
>
[IO example]
>

What do you mean it's not lazy ?
look :

> import Control.Monad
> import Control.Monad.State.Lazy
>
> inc x = modify (+x) >> get
>
> main = do
>   let (pows,_) = runState (liftM (take 5) $ iterateM inc 1) $ 0
>   print pows
>
....

In other words, the lazyness is not in iterateM, it's in the monad
(you can use unsafeInterleaveIO to get lazy IO but that's... unsafe).

>
> 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 :-)
>

Basically IO is strict, if it wasn't, you wouldn't always get the
correct order for your side-effects except when explicit data
dependency forced it (unsafeInterleaveIO is a way to relax this when
you don't care exactly when your side-effect happens, for instance
lazy IO take the stance that you don't need to know exactly when your
file will be read, so you might as well defer its reading until the
content is really needed).

-- 
Jeda?



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

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


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

Reply via email to