iterated function value sequence

What's a good way to produce an iterated function value sequence? That is, f(x), f(f(x)), f(f(f(x))), etc. ref: https://www.youtube.com/watch?v=09JslnY7W_k

There's a function named iterate in Prelude:
λ> :info iterate
iterate :: (a -> a) -> a -> [a] -- Defined in `GHC.List'
λ> take 8 (iterate (\x -> 2 * x + 1) 0)
[0,1,3,7,15,31,63,127]
On Fri, Apr 4, 2014 at 1:25 PM, John M. Dlugosz
What's a good way to produce an iterated function value sequence? That is, f(x), f(f(x)), f(f(f(x))), etc.
ref: https://www.youtube.com/watch?v=09JslnY7W_k
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On 4/4/2014 3:35 PM, Bob Ippolito wrote:
There's a function named iterate in Prelude:
λ> :info iterate iterate :: (a -> a) -> a -> [a] -- Defined in `GHC.List' λ> take 8 (iterate (\x -> 2 * x + 1) 0) [0,1,3,7,15,31,63,127]
Nice. I followed up with that to understand how it's written. Now, how might I do something like that but "forget" previous values to free up memory? —John

On Saturday, April 5, 2014, John M. Dlugosz
On 4/4/2014 3:35 PM, Bob Ippolito wrote:
There's a function named iterate in Prelude:
λ> :info iterate iterate :: (a -> a) -> a -> [a] -- Defined in `GHC.List' λ> take 8 (iterate (\x -> 2 * x + 1) 0) [0,1,3,7,15,31,63,127]
Nice. I followed up with that to understand how it's written.
Now, how might I do something like that but "forget" previous values to free up memory?
Garbage Collection does that for you. If all the references are gone, the memory can be freed. If you have a specific use in mind I can show you what that looks like.

On 4/5/2014 10:55 AM, Bob Ippolito wrote:
Now, how might I do something like that but "forget" previous values to free up memory?
Garbage Collection does that for you. If all the references are gone, the memory can be freed. If you have a specific use in mind I can show you what that looks like.
Say, zz = abs <$> numerator <$> iterate (\x->x^2-7%4) 0 and at some point I print $ zz!!30 How does the GC know that I'll never refer to zz!!n again where n < 30 ?

If zz is top-level, then it doesn't know you'll never refer to it again
(the compiler doesn't *have* to keep this around it's hard to know whether
or not it will).
λ> let zz = abs <$> numerator <$> iterate (\x->x^2-7%4) 0
λ> print (zz !! 2)
21
λ> :sprint zz
zz = _ : _ : 21 : _
Note also that iterate is best when you use all of the values up to where
you want to stop (or at least make sure they get evaluated).
If you want to iterate a specific number of times and just get the result,
perhaps foldl' is the way to do it.
λ> abs $ numerator $ foldl' (\x _ -> x^2-7%4) 0 (replicate 7 ())
595096023596888261906480183623645954687
See also:
https://ghc.haskell.org/trac/ghc/ticket/3474
http://stackoverflow.com/questions/8909997/haskell-repeat-a-function-a-large...
On Sun, Apr 6, 2014 at 12:44 AM, John M. Dlugosz
On 4/5/2014 10:55 AM, Bob Ippolito wrote:
Now, how might I do something like that but "forget" previous values to free up memory?
Garbage Collection does that for you. If all the references are gone, the memory can be freed. If you have a specific use in mind I can show you what that looks like.
Say,
zz = abs <$> numerator <$> iterate (\x->x^2-7%4) 0
and at some point I print $ zz!!30
How does the GC know that I'll never refer to zz!!n again where n < 30 ?
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (2)
-
Bob Ippolito
-
John M. Dlugosz