First have a look at delay (and clojure.lang.Delay) and try to understand
it. LazySeq is just a delay with a fancy interface (supporting seq, first
and rest). There is no recursion per se in the LazySeq object. What makes
it harder to understand is the idiom of using lazy-seq together with cons
and recursion, as seen e.g. in following range-like function:

(defn range*
  [from] (lazy-seq (cons from (range* (inc from)))))

user=> (set! *print-length* 10)
10
user=> (range* 0)
(0 1 2 3 4 5 6 7 8 9 ...)


Similar function with delay would be

(defn range*
  [from] (delay [from (range* (inc from))]))

user=> (range* 0)
#<Delay@7c5ff8ce: :pending>
user=> @(range* 0)
[0 #<Delay@19c6a644: :pending>]
user=> (first @(range* 0))
0
user=> (second @(range* 0))
#<Delay@672f151d: :pending>
user=> @(second @(range* 0))
[1 #<Delay@3e6dac3d: :pending>]
user=> (first @(second @(range* 0)))
1
...

Jozef


On Sun, Apr 6, 2014 at 6:56 PM, sorin cristea <srncris...@gmail.com> 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.
>

-- 
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