Lazy evaluation is a harsh mistress.

user=> (def b1 (binding [*num* 1024] (f1)))
#'user/b1
user=> (def b2 (binding [*num* 1024] (f1)))
#'user/b2
user=> b1
(16)
user=> b2
(16)

The difference between the example above and your example is the  
interaction with the REPL. Your f1 is lazy, and is not realized until  
outside the binding, when the REPL needs it. Your f2, also lazy, is  
realized inside the binding because of the call to first.

Cheers,
Stuart

> Hi all. Is this a bug or a feature? When I bind the var *num* to a new
> value, in one case it appears the lambda uses the original value, not
> the newly bound one.
>
> (def *num* 16)
>
> (defn f1 []
> ( map (fn [x]  *num* ) [1]))
>
>
> (defn f2 []           ;; same as f1 but calls first on the map
> (first
>  ( map (fn [x]  *num* ) [1])))
>
>
> user> (f1)
> (16)                                                       <--ok
> user> (f2)
> 16                                                         <--ok
> user> (binding [*num* 1024] (f1))
> (16)                                                      <--  
> expected 1024
> user> (binding [*num* 1024] (f2))
> 1024                                                     <--ok after
> all, in spite of the fact the above gave list (16)
>
> Thanks,
> Hugh
>
> >


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