> 1) I assumed that printing out [i @a] instead of [@a i] should realize 'i'
> first and @a should be correctly displayed as 5. This does not happen, it
> simply prints [(1 2 3 4 5) 0] if the order is reversed.

So, this evaluates in two "stages".

First the terms `i` and `@a` are evaluated to get their values, so `i`
returns the lazy sequence it contains (not realised at all) and `@a`
returns the value inside the atom (which is 0).

Then the printing statement goes through each of those values and
tries to work out what it needs to print. For `i` this involves
realising the sequence (to get each value to print), but for `@a` it's
already got the value (0, from earlier), so it just prints that. For
the lazy sequence it didn't have a printable value, it just had
something saying "this is a sequence that will be generated when you
want it", so it has to do more work before it can print.

> 2) If I put a breakpoint in the predicate for take-while, it gets hit ( I
> was expecting it to not get hit if it was lazy)

The lazy sequence still gets realised, just not when you'd expect, so
you still hit the breakpoint, just after you've already retrieved the
value of the atom.

> 3) This is the strangest observation of all: In the debugger I can see that
> 'a' is getting incremented, its changing when the breakpoint is hit!  but
> the baffling thing is, when the result is printed out, I still get 0 as the
> value for a.

This is also explained by the above.


You're basically getting stung by precisely when a lazy sequence gets
realised. This sort of issue is why you should try to avoid having any
sort of side effects in the generation of lazy sequences.

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

Reply via email to