Hello, (take 1 fib-seq) => (1)
Which can also be seen as[*] (map + (0) (1)) (map + '(0) '(1)) => (1) Makes sense? (take 2 fib-seq) => (1 1) Here the recursive definition (note that it's not a function, fib-seq simply a Var that holds a LazySeq object) comes into play. We already know the first element in the sequence, so: (map + '(0 0) '(1 1)) => (1 1) The second 1 there is the first element of the fib-seq. *: I'm using vague language intentionally here. Anyone who knows the right term please correct me. On Sun, Mar 9, 2014 at 12:54 PM, Asfand Yar Qazi <ayq...@gmail.com> wrote: > Hi, > > I'm trying to understand the following function (from > http://en.wikibooks.org/wiki/Clojure_Programming/Examples/Lazy_Fibonacci#Self-Referential_Version > ): > > (def fib-seq > (lazy-seq > (map + > (cons 0 (cons 0 fib-seq)) > (cons 1 fib-seq)))) > > I'm trying to understand how this works. In particular, I do not > understand what the recursive call to fib-seq will return when the sequence > is lazily evaluated. > > Here's my understanding so far: > > The first time fib-seq is invoked, it has no head, and the function is the > tail. So we go into the first collection, where we append 0 and 0 to > fib-seq, which then '(0 0) . This is then mapped with the second > collection, which (because fib-seq has not returned anything yet) is '(1) . > Shouldn't map then raise an error because it is effectively being called > as (map + (0 0) (1)) ? > > I would be very grateful for any insights. > > Thanks > > > -- > 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. > -- Kind Regards, Atamert Ölçgen -+- --+ +++ www.muhuk.com -- 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.