Here's some simple code that demonstrates evaluation of items in an
infinite, lazy sequence. The function f contains a println side-effect
so I know when it's being called.

(defn f
  "square the parameter and divide by 2"
  [x]
  (println "calculating f of" x)
  (/ (* x x) 2.0))

; Create an infinite, lazy sequence of f values for 0 through infinity.
(def f-infinite-seq (map f (iterate inc 0))) ; values 0 through infinity

(println "The first is" (first f-infinite-seq))
(println "The third is" (nth f-infinite-seq 2))

The output from this code follows:

calculating f of 0
The first is 0.0
calculating f of 1
calculating f of 2
The third is 2.0

I'm trying to understand why I don't see a second line that says
"calculating f of 0" immediately before the line that says
"calculating f of 1". This is implying to me that the result from (f
0) got cached and the function didn't have to be called again. I'm not
holding onto that first result though, so why is it being cached? I'm
concerned that if it's caching results, I could run out of memory if I
need to evaluate an item that is really deep into the infinite
sequence. Do I need to construct the infinite sequence differently to
prevent result caching?

-- 
R. Mark Volkmann
Object Computing, Inc.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to