Hi Ginaluca, 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.
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. On Monday, April 7, 2014 1:45:41 AM UTC+3, gianluca torta wrote: > > Hi sorin, > > your function computes a sequence of just one element (the sum of the > collection members), so I would say it is not a typical use of (lazy) seqs > > to see that the code is indeed lazy, you can try: > (def x (test-fc (range 210432423543654675765876879))) > and see that it returns immediately; however, as soon as you evaluate x: > x > the whole computation of the only element of x starts and continues all > the way to process the 210432423543654675765876879 elements of the range > > As for your question on recursion: this is an example of tail recursion, > that is automatically converted into plain iteration. > When there is only a recursive call, and such a call is the last thing in > the body of the function, it can be easily (and automatically) converted to > iteration, with no stack overflow problems. > > hope this helps > Gianluca > > > > On Sunday, April 6, 2014 6:56:00 PM UTC+2, sorin cristea wrote: >> >> >> Hi, >> >> maybe this question was already put it here, but can someone explain how >> exactly work internal a function wrapped with lazy-seq keyword. For example >> in the below code sample: >> >> ( >> defn test-fc >> "sum of all collection elements using recursion and laziness" >> [coll] >> (letfn [(sum-fc [sum coll] >> (if (empty? coll) >> (cons sum nil) >> (lazy-seq(sum-fc (+ sum (first coll)) (rest coll)))) >> >> ) >> ] >> (sum-fc 0 coll) >> ) >> ) >> >> if I test the function: (test-fc (range 5)) I got the right result, if I >> continue to test with bigger number I don't got StackoverflowException, but >> if run (test-fc (range 210432423543654675765876879)) I didn't get >> StackoverflowEx but the application didn't return any result, because take >> to much time to compute this ? >> >> How exactly work this internally and how is removed recursion( inside >> call sum-fc with new parameters) from the flow in this case ? From what I >> saw in java code of LazySeq class and clojure source code, it's made a list >> with LazySeq object and in my opinion in that list the LazySeq object >> contain enough information to compute the requested item and in this way is >> removed recursion and implicit StackoverflowException issue, if I >> understand something wrong please explain to me. >> I test this function from IntellijIDEA + LaClojure plugin. >> >> Thanks >> 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.