I was experimenting with how binding behaves within a loop and found
some inconsistent results:

(def y 0)
(loop [x 0]
  (println "x " x)
  (binding [y (inc y)]
    (println "y " y)
    (if (< x 10) (recur (inc x)))))

The printed lines are what you'd expect:
x  0
y  1
x  1
...
x  10
y  11

But if you then check the value of y afterwards, it is 3.  Each time
the above loop is executed, y goes up by another 3.  I'm not sure how
I can see the whole stack of bound values, but I did confirm the root
is still 0.

Oddly, the following /does not/ result in left-over bindings:
(loop [x 5]
  (binding [y (inc y)]
  (println "x " x ", y " y)
  (if (> x 0) (recur (dec x)))))

Any ideas on the differences?

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