[Haskell-cafe] laziness

jeff p mutjida at gmail.com
Sun Dec 10 01:57:37 EST 2006


Hello,

> So I'm clearly expecting ghc to be more psychic than it's currently
> capable.  But what I'd like to do is pass an infinite list through a
> processing function which maintains some state (foo could easily be a
> pseudo random number generator), and take the first bit of it.
>
> Any help?
>
Intuitively, your state is flowing from the end of the list back
towards the beginning in your code.

If you really want a right fold where the state flows from left to
right, you could try this:

    foldrSt st f v [] = v st
    foldrSt st f v (x:xs) =
        let (st', f') = f st x
        in f' (foldrSt st' f v xs)

and then rewrite foo as:

    foo payload x = (payload, (\l -> x:l))

then:

    take 10 $ foldrSt Nothing foo (\st -> []) [1..]

evaluates to [1 .. 10].

-Jeff


More information about the Haskell-Cafe mailing list