The point was you aren't using lazy-seq as intended here since you are 
always creating a singleton sequence. What's going on behind the scenes 
here is in effect just trampolining thunks.

(defn thunked-sum [sum coll]
  (if-let [[x & more] (seq coll)] 
    (fn [] (thunked-sum (+ sum x) more))
    sum))

(trampoline (thunked-sum 0 (range 100000))) ;=> 4999950000

The "trampoline" portion of your lazy seq is the while loop in this part of 
the Java implementation of LazySeq

final synchronized public ISeq seq(){
        sval();
        if(sv != null)
                {
                Object ls = sv;
                sv = null;
                while(ls instanceof LazySeq)
                        {
                        ls = ((LazySeq)ls).sval();
                        }
                s = RT.seq(ls);
                }
        return s;
}


If you do (test-fc (range 210432423543654675765876879)) at your REPL, 
evaluation is forced for the print, but because your input is so large, the 
calculation time is prohibitively long. If you placed this in a def 
instead, evaluation would be delayed until requested at which point it 
would then take prohibitively long to complete.

On Monday, April 7, 2014 3:01:54 PM UTC-5, sorin cristea wrote:
>
>
> Hi Gianluca, 
>
>  I have a question ; why when a run/execute command/code line (test-fc 
> (range 210432423543654675765876879)) it's not executed the function 
> test-fc and return the sum for all 210432423543654675765876879 elements? 
> why should I put the test-fc reference to a variable, x, like you present 
> below. ( this is related to your phrase - "your function computes a 
> sequence of just one element (the sum of the collection members" - why ?)
>
>  In this case (def x (test-fc (range 210432423543654675765876879)) I see 
> here a problem, I keep a reference to the head of sequence and this will 
> imply that the GC will can't garbage the unused items, if is wrong what I'm 
> say please correct me. 
>
>  thanks a lot
>  Sorin.
>

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to