Hi, Tim, I'm not 100% certain what is going on here, but I do know that, in general, binding and loop/recur should not be mixed.
"recur" is not true recursion -- it's more like a GOTO. The "binding" macro establishes thread-local bindings using the static methods clojure.lang.Var/pushThreadBindings and popThreadBindings. So what I think is happening in your first example is that the "recur" jumps back to the top of the loop before "popThreadBindings" gets called. I have no idea why it only happens three times, or why it doesn't happen in your second example. Here's a demonstration: user> (loop [x 0] (println "x" x) (binding [y (inc y)] (println "y" y) (if (< x 10) (recur (inc x))))) x 0 ... user> y 3 user> (clojure.lang.Var/popThreadBindings) nil user> y 2 user> (clojure.lang.Var/popThreadBindings) nil user> y 1 user> (clojure.lang.Var/popThreadBindings) nil user> y 0 user> -SS On Jul 17, 10:38 pm, Tim Snyder <tsnyder...@gmail.com> wrote: > 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 -~----------~----~----~----~------~----~------~--~---