On 10 March 2014 10:26, Asfand Yar Qazi <ayq...@gmail.com> wrote:
> Hi,
>
> On Sunday, 9 March 2014 14:58:47 UTC, Atamert Ölçgen wrote:
>>
>> Hello,
>>
>> (take 1 fib-seq) => (1)
>>
>>
>> Which can also be seen as[*] (map + (0) (1))
>>
>> (map + '(0) '(1)) => (1)
>>
>>
>> Makes sense?
>>
>
> I'm afraid it still doesn't make sense; I still don't understand how (cons 0
> (cons 0 fib-seq)) evaluates to '(0) the first time; it should evaluate to
> '(0 0) since there are 2 cons statements.  That's the point I'm stuck on;
> any help would be gratefully received.

The code is this:

(def fib-seq
  (lazy-seq
    (map +
      (cons 0 (cons 0 fib-seq))
      (cons 1 fib-seq))))

According to the documentation for map
http://clojuredocs.org/clojure_core/clojure.core/map
(map + x y)

where x and y are two collections adds the first element of x to the
first element of y, the second element of x to the second element of y
and so on until either x or y is exhausted.

The documentation for cons states that (cons x seq) returns a new
collection with x as the first element and seq as the rest so (cons 0
[1 2 3 4]) is '(0 1 2 3 4) and  (cons 0 (cons 0 [1 2 3 4])) is the
same as (cons 0 [0 1 2 3 4]) which gives '(0 0 1 2 3 4).

So the expression

(cons 0 (cons 0 fib-seq))

is the same as fib-seq with two zeros in front. It is not the same as
fib-seq with '(0 0) in front.

When the map maps over (cons 0 (cons 0 fib-seq)) and takes its first
element, it finds that the first element is 0, not '(0 0).

You seem to be trying to imagine how lazy-seqs work rather than
reading the documentation, which tells you how they behave when you
run a program or type an expression into the REPL. This is a bad idea
since it conflates the implementation with the behaviour. The
behaviour is in the documentation. If you want to know how functional
data structures work there are books about this kind of thing like
"Purely Functional Data Structures" by Okasaki.

Alan

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