On May 18, 2009, at 11:58, Adrian Cuthbertson wrote:

> I know I keep plugging this - sorry - but it just keeps surfacing  
> as a solution;
>
> (lcm 4 6)
> 12
> (binding [clojure.contrib.math/gcd (fn [a b] 1)] (lcm 4 6))
> 24

Such a use of binding will lead to bad surprises as soon as you use  
it with lazy sequences:

(map #(lcm % 6) (range 6))
-> (0 6 6 6 12 30)

(binding [clojure.contrib.math/gcd (fn [a b] 1)]
   (map #(lcm % 6) (range 6)))
-> (0 6 6 6 12 30)

You have to use doall to get the result you expect:

(binding [clojure.contrib.math/gcd (fn [a b] 1)]
   (doall (map #(lcm % 6) (range 6))))
-> (0 6 12 18 24 30)

The reason is that the temporary binding remains active only while  
map creates the lazy sequence that it returns, but it is no longer  
active when the actual elements of the lazy sequence are evaluated.

More details and examples in my blog post:
   http://onclojure.com/2009/05/06/simulating-dynamic-scoping/

Konrad.


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