let binding for functions
I'm a clojure newbie. Why does the compiler complain about being unable to resolve cube-root-recur on line 17? thanks 1 (def cube-root 2 (fn [n] 3 (let 4 [abs 5(fn [n] 6 (if ( n 0) n (- 0 n))) 7good-enough? 8(fn [guess] 9 ( (abs (- (* guess guess guess) n)) 0.0001)) 10improve-guess 11(fn [guess] 12 (/ (+ (/ n (* guess guess)) (* 2 guess)) 3)) 13cube-root-recur 14(fn [guess] 15 (cond 16(good-enough? guess) guess 17:else (cube-root-recur (improve-guess guess] 18 (cube-root-recur 1 -- 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
Re: let binding ~'=
it is a macro with a syntax quote, so all symbols are namespace-qualified. so = will actually be clojure.core/= the ~' removes this namespace-qualification, so it is rendered as just =. 2010/8/21 limux liumengji...@gmail.com: This is some code in a blog of William Gropper, the useage of ~'= confused me, waiting some more detailed explain, advanced thanks (defmacro filter [pred query] `(let [~'and (fn[ xs#] (apply str (interpose AND xs#))) ~'or (fn[ xs#] (apply str (interpose OR xs#))) ~'= (fn[x# y#] (sql-exp = x# y#)) ~' (fn[x# y#] (sql-exp x# y#)) ~' (fn[x# y#] (sql-exp x# y#)) ~'like (fn[x# y#] (sql-exp like x# y#)) ~'in (fn[x# xs#] (str (sqlize x#) IN ( (apply str (interpose , xs#)) )))] (apply str ~query where ~pred))) -- 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 -- Linux-user #496644 (http://counter.li.org) - first touch of linux in 2004 Demandoj en aŭ pri Esperanto? Questions about Esperanto? Vragen over Esperanto? Perguntas sobre o Esperanto? - http://demandoj.tk -- 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
Re: let binding ~'=
Clojure whitin a `(...) context resolves the name space of symbols. This is most often what you want and prevent some name collisions at macro expension. But as binders can not be name-space resolved this do not allow to write: `(let [and ]) this would expand to (let [ns/and ...]) which is forbidden. The usual solutions (choose the first applicable) : - use automatic name generation `(let [and# ] and#) - generate a symbol name: (let [my-fresh-sym (gensym foo_)] `(let [~my-fresh-sym ...]) It is mostly useful when the same symbols occurs in a complex structure of `(...)s. - If you really need to choose the name a binding symbol: use ~'symbol. This says to the evaluation of `() replace by exactly symbol So (`let [~'and ...]) will expand to (let [and ...] ) this is a problem because it can conflict with some other names. However, in some case it is very useful/necessary. Especially when you want to expose some symbols to the macro user. On Sat, Aug 21, 2010 at 3:20 PM, limux liumengji...@gmail.com wrote: This is some code in a blog of William Gropper, the useage of ~'= confused me, waiting some more detailed explain, advanced thanks (defmacro filter [pred query] `(let [~'and (fn[ xs#] (apply str (interpose AND xs#))) ~'or (fn[ xs#] (apply str (interpose OR xs#))) ~'= (fn[x# y#] (sql-exp = x# y#)) ~' (fn[x# y#] (sql-exp x# y#)) ~' (fn[x# y#] (sql-exp x# y#)) ~'like (fn[x# y#] (sql-exp like x# y#)) ~'in (fn[x# xs#] (str (sqlize x#) IN ( (apply str (interpose , xs#)) )))] (apply str ~query where ~pred))) -- 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 -- 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
Re: let binding ~'=
Is where a canonical doc about ~' or I have to read the core.clj? On 8月21日, 下午10时40分, Nicolas Oury nicolas.o...@gmail.com wrote: Clojure whitin a `(...) context resolves the name space of symbols. This is most often what you want and prevent some name collisions at macro expension. But as binders can not be name-space resolved this do not allow to write: `(let [and ]) this would expand to (let [ns/and ...]) which is forbidden. The usual solutions (choose the first applicable) : - use automatic name generation `(let [and# ] and#) - generate a symbol name: (let [my-fresh-sym (gensym foo_)] `(let [~my-fresh-sym ...]) It is mostly useful when the same symbols occurs in a complex structure of `(...)s. - If you really need to choose the name a binding symbol: use ~'symbol. This says to the evaluation of `() replace by exactly symbol So (`let [~'and ...]) will expand to (let [and ...] ) this is a problem because it can conflict with some other names. However, in some case it is very useful/necessary. Especially when you want to expose some symbols to the macro user. On Sat, Aug 21, 2010 at 3:20 PM, limux liumengji...@gmail.com wrote: This is some code in a blog of William Gropper, the useage of ~'= confused me, waiting some more detailed explain, advanced thanks (defmacro filter [pred query] `(let [~'and (fn[ xs#] (apply str (interpose AND xs#))) ~'or (fn[ xs#] (apply str (interpose OR xs#))) ~'= (fn[x# y#] (sql-exp = x# y#)) ~' (fn[x# y#] (sql-exp x# y#)) ~' (fn[x# y#] (sql-exp x# y#)) ~'like (fn[x# y#] (sql-exp like x# y#)) ~'in (fn[x# xs#] (str (sqlize x#) IN ( (apply str (interpose , xs#)) )))] (apply str ~query where ~pred))) -- 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 -- 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
Re: let binding ~'=
Thanks, I remindered that it's a reader macro used to get the symbol itself exactly without namespace. Ok, I see! On 8月21日, 下午10时58分, limux liumengji...@gmail.com wrote: Is where a canonical doc about ~' or I have to read the core.clj? On 8月21日, 下午10时40分, Nicolas Oury nicolas.o...@gmail.com wrote: Clojure whitin a `(...) context resolves the name space of symbols. This is most often what you want and prevent some name collisions at macro expension. But as binders can not be name-space resolved this do not allow to write: `(let [and ]) this would expand to (let [ns/and ...]) which is forbidden. The usual solutions (choose the first applicable) : - use automatic name generation `(let [and# ] and#) - generate a symbol name: (let [my-fresh-sym (gensym foo_)] `(let [~my-fresh-sym ...]) It is mostly useful when the same symbols occurs in a complex structure of `(...)s. - If you really need to choose the name a binding symbol: use ~'symbol. This says to the evaluation of `() replace by exactly symbol So (`let [~'and ...]) will expand to (let [and ...] ) this is a problem because it can conflict with some other names. However, in some case it is very useful/necessary. Especially when you want to expose some symbols to the macro user. On Sat, Aug 21, 2010 at 3:20 PM, limux liumengji...@gmail.com wrote: This is some code in a blog of William Gropper, the useage of ~'= confused me, waiting some more detailed explain, advanced thanks (defmacro filter [pred query] `(let [~'and (fn[ xs#] (apply str (interpose AND xs#))) ~'or (fn[ xs#] (apply str (interpose OR xs#))) ~'= (fn[x# y#] (sql-exp = x# y#)) ~' (fn[x# y#] (sql-exp x# y#)) ~' (fn[x# y#] (sql-exp x# y#)) ~'like (fn[x# y#] (sql-exp like x# y#)) ~'in (fn[x# xs#] (str (sqlize x#) IN ( (apply str (interpose , xs#)) )))] (apply str ~query where ~pred))) -- 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 -- 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
Re: let binding ~'=
On Aug 21, 9:58 am, limux liumengji...@gmail.com wrote: Is where a canonical doc about ~' or I have to read the core.clj? ~' is not an actual thing. Note that `~expr is the same as expr, so `~'foo is the same as 'foo is the same as (quote foo), which when evaluated yields the symbol foo. -- 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
Re: let binding and recursive function
Hello John, I would want to add something to what Justin said. (defn goo [ i k l] (println l)) = (goo 2 3 [233]) ;;; gives ([233]) (conj '([233]) ...) (defn goo [i k [l]](println l)) =(goo 2 3 [233]) ;;; gives [233] (conj [233] ...) Hope the difference is clear now. Emeka On Mon, Aug 2, 2010 at 10:43 PM, John Sanda john.sa...@gmail.com wrote: Thanks for the response and suggestions Justin. A co-worker also just suggested multiple arg lists which is perfect. He also suggested (or foo bar) to further simplify my code. It definitely cleans the code up and improves readability. - John On Mon, Aug 2, 2010 at 5:37 PM, Justin Kramer jkkra...@gmail.com wrote: I think you want: (defn- do-traversal [tree idx [tree-traversal]] ...) Note the extra brackets for destructuring. Another alternative is using multiple arg lists: (defn- do-traversal ([tree idx] (do-traversal tree idx [])) ([tree idx traversal] ...)) Lastly, FYI, the form (if foo foo bar) can be simplified to (or foo bar). Hope that helps, Justin On Aug 2, 5:04 pm, John Sanda john.sa...@gmail.com wrote: I've just implemented an inorder traversal function for a vector-based tree. The functions look like, (defn- do-traversal [tree idx traversal] (cond (not (node-exists? tree idx)) traversal (leaf? tree idx) (conj traversal (tree idx)) :else (apply conj (do-traversal tree (left-child idx) traversal) (tree idx) (do-traversal tree (right-child idx) traversal (inorder-traversal [tree] (do-traversal tree root-idx [])) This works as expected but now I am looking to refactor the code some. I wanted to see if I could do away passing an empty vector to the do-traversal function. So I updated do-traversal to look like, (defn- do-traversal [tree idx tree-traversal] (let [traversal (if tree-traversal tree-traversal [])] (cond (not (node-exists? tree idx)) traversal (leaf? tree idx) (conj traversal (tree idx)) :else (apply conj (do-traversal tree (left-child idx) traversal) (tree idx) (do-traversal tree (right-child idx) traversal) When the expected traversal for a tree is [10 20 30] I instead get ([] 30 20 10 [])) in my unit test. Can someone explain to me why using let as I have done does not work, and what another solution might be? Thanks - John -- 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.comclojure%2bunsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -- - John -- 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.comclojure%2bunsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -- 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
let binding and recursive function
I've just implemented an inorder traversal function for a vector-based tree. The functions look like, (defn- do-traversal [tree idx traversal] (cond (not (node-exists? tree idx)) traversal (leaf? tree idx) (conj traversal (tree idx)) :else (apply conj (do-traversal tree (left-child idx) traversal) (tree idx) (do-traversal tree (right-child idx) traversal (inorder-traversal [tree] (do-traversal tree root-idx [])) This works as expected but now I am looking to refactor the code some. I wanted to see if I could do away passing an empty vector to the do-traversal function. So I updated do-traversal to look like, (defn- do-traversal [tree idx tree-traversal] (let [traversal (if tree-traversal tree-traversal [])] (cond (not (node-exists? tree idx)) traversal (leaf? tree idx) (conj traversal (tree idx)) :else (apply conj (do-traversal tree (left-child idx) traversal) (tree idx) (do-traversal tree (right-child idx) traversal) When the expected traversal for a tree is [10 20 30] I instead get ([] 30 20 10 [])) in my unit test. Can someone explain to me why using let as I have done does not work, and what another solution might be? Thanks - John -- 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
Re: let binding and recursive function
I think you want: (defn- do-traversal [tree idx [tree-traversal]] ...) Note the extra brackets for destructuring. Another alternative is using multiple arg lists: (defn- do-traversal ([tree idx] (do-traversal tree idx [])) ([tree idx traversal] ...)) Lastly, FYI, the form (if foo foo bar) can be simplified to (or foo bar). Hope that helps, Justin On Aug 2, 5:04 pm, John Sanda john.sa...@gmail.com wrote: I've just implemented an inorder traversal function for a vector-based tree. The functions look like, (defn- do-traversal [tree idx traversal] (cond (not (node-exists? tree idx)) traversal (leaf? tree idx) (conj traversal (tree idx)) :else (apply conj (do-traversal tree (left-child idx) traversal) (tree idx) (do-traversal tree (right-child idx) traversal (inorder-traversal [tree] (do-traversal tree root-idx [])) This works as expected but now I am looking to refactor the code some. I wanted to see if I could do away passing an empty vector to the do-traversal function. So I updated do-traversal to look like, (defn- do-traversal [tree idx tree-traversal] (let [traversal (if tree-traversal tree-traversal [])] (cond (not (node-exists? tree idx)) traversal (leaf? tree idx) (conj traversal (tree idx)) :else (apply conj (do-traversal tree (left-child idx) traversal) (tree idx) (do-traversal tree (right-child idx) traversal) When the expected traversal for a tree is [10 20 30] I instead get ([] 30 20 10 [])) in my unit test. Can someone explain to me why using let as I have done does not work, and what another solution might be? Thanks - John -- 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
Re: let binding and recursive function
Thanks for the response and suggestions Justin. A co-worker also just suggested multiple arg lists which is perfect. He also suggested (or foo bar) to further simplify my code. It definitely cleans the code up and improves readability. - John On Mon, Aug 2, 2010 at 5:37 PM, Justin Kramer jkkra...@gmail.com wrote: I think you want: (defn- do-traversal [tree idx [tree-traversal]] ...) Note the extra brackets for destructuring. Another alternative is using multiple arg lists: (defn- do-traversal ([tree idx] (do-traversal tree idx [])) ([tree idx traversal] ...)) Lastly, FYI, the form (if foo foo bar) can be simplified to (or foo bar). Hope that helps, Justin On Aug 2, 5:04 pm, John Sanda john.sa...@gmail.com wrote: I've just implemented an inorder traversal function for a vector-based tree. The functions look like, (defn- do-traversal [tree idx traversal] (cond (not (node-exists? tree idx)) traversal (leaf? tree idx) (conj traversal (tree idx)) :else (apply conj (do-traversal tree (left-child idx) traversal) (tree idx) (do-traversal tree (right-child idx) traversal (inorder-traversal [tree] (do-traversal tree root-idx [])) This works as expected but now I am looking to refactor the code some. I wanted to see if I could do away passing an empty vector to the do-traversal function. So I updated do-traversal to look like, (defn- do-traversal [tree idx tree-traversal] (let [traversal (if tree-traversal tree-traversal [])] (cond (not (node-exists? tree idx)) traversal (leaf? tree idx) (conj traversal (tree idx)) :else (apply conj (do-traversal tree (left-child idx) traversal) (tree idx) (do-traversal tree (right-child idx) traversal) When the expected traversal for a tree is [10 20 30] I instead get ([] 30 20 10 [])) in my unit test. Can someone explain to me why using let as I have done does not work, and what another solution might be? Thanks - John -- 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.comclojure%2bunsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -- - John -- 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
Re: special forms and let binding
Hello ataggart, thank you for the correction! Only now I understand A.Rosts question. May be somebody can help, and explain why my hypothesis was wrong. Obviousely, while functions are first class, special forms are even better, kind of zeroth class. Thank you, alux On 31 Mai, 04:15, ataggart alex.tagg...@gmail.com wrote: On May 30, 12:45 pm, alux alu...@googlemail.com wrote: Hi A., I dont completely understand what you refer to with works correct. You define a local variable, named do, and use it. That works of course. Btw you may use it without the call to var (def do println) (do example) It only appears to work; the second line is still using the special form. You can see this more clearly here: user= (def if println) #'user/if user= (if hi) java.lang.Exception: Too few arguments to if (NO_SOURCE_FILE:22) user= ((var if) hi) hi nil The hi is from the println function, the nil is what was returned from the call. -- 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
Re: special forms and let binding
In fact, keywords are not symbols. So thats why you were wrong. You can read about in on the page http://clojure.org/lisps -- 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
Re: special forms and let binding
So I don't understand if there any way do it. I'm really curious about it. -- 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
Re: special forms and let binding
Hi Аркадий, I started another thread about the difference between special form and macros today - and got told that it is not possible to overwrite a special form. Regards, alux On 31 Mai, 21:15, Ark. Rost arkr...@gmail.com wrote: So I don't understand if there any way do it. I'm really curious about it. -- 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
special forms and let binding
Hi! For example, it's possible to do things like: (def do println) ((var do) example) And it works correct. But I don't understand how to get the same behavior in let bindings. I mean (let [do println] ..) what can I write to get the same results? -- 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
Re: special forms and let binding
Hi A., I dont completely understand what you refer to with works correct. You define a local variable, named do, and use it. That works of course. Btw you may use it without the call to var (def do println) (do example) In let you may do (let [do println] (do :plop)) Is this what you want? (I'm driven to point out that this is bad style - but you probably know already.) Regards, alux On 30 Mai, 19:07, A.Rost aravanc...@gmail.com wrote: Hi! For example, it's possible to do things like: (def do println) ((var do) example) And it works correct. But I don't understand how to get the same behavior in let bindings. I mean (let [do println] ..) what can I write to get the same results? -- 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
Re: special forms and let binding
On May 30, 12:45 pm, alux alu...@googlemail.com wrote: Hi A., I dont completely understand what you refer to with works correct. You define a local variable, named do, and use it. That works of course. Btw you may use it without the call to var (def do println) (do example) It only appears to work; the second line is still using the special form. You can see this more clearly here: user= (def if println) #'user/if user= (if hi) java.lang.Exception: Too few arguments to if (NO_SOURCE_FILE:22) user= ((var if) hi) hi nil The hi is from the println function, the nil is what was returned from the call. -- 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
Re: let-binding overrides binding-binding
Tom Hicks hickstoh...@gmail.com writes: I can't seem to do this with the 'inc' function: user= (binding [inc (fn [y] (+ 2 y))] (inc 44)) 45 Why doesn't this work? See this thread: http://groups.google.com/group/clojure/browse_thread/thread/d772e114e50aace6 -- 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
Re: let-binding overrides binding-binding
Actually, I think shadowing core functions is not a great idea Quite so! Common Lisp expressly prohibits this stuff for the COMMON- LISP package. but I wanted to understand the principles involved here. Thanks again for your rapid help. My pleasure. Happy Christmas! -- 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
Re: let-binding overrides binding-binding
On a related note, can someone explain the following... I can define a function 'p1': user= (defn p1 [x] (+ 1 x)) #'user/p1 user= (p1 44) 45 and then shadow it within the binding construct: user= (binding [p1 (fn [y] (+ 2 y))] (p1 44)) 46 but, I can't seem to do this with the 'inc' function: user= (binding [inc (fn [y] (+ 2 y))] (inc 44)) 45 Why doesn't this work? Stephen said previously The binding form operates on the var, it doesn't affect name resolution within the binding form's body so the (inc 44) must resolve to clojure.core/inc: user= (binding [inc (fn [y] (+ 2 y))] (resolve 'inc)) #'clojure.core/inc but, is there a way to shadow the var pointed to by clojure.core/inc? Neither of the following works: user= (binding [#'clojure.core/inc (fn [y] (+ 2 y))] (inc 44)) java.lang.ClassCastException: clojure.lang.Cons (NO_SOURCE_FILE:22) user= (binding [clojure.core/inc (fn [y] (+ 2 y))] (clojure.core/inc 44)) 45 Any help untangling the concepts of vars, namespaces, and binding interactions is appreciated, -tom On Dec 19, 5:21 pm, Stephen C. Gilardi squee...@mac.com wrote: On Dec 19, 2009, at 5:23 PM, Stefan Kamphausen wrote: 1. Is my explanation correct? It is. The binding form operates on the var, it doesn't affect name resolution within the binding form's body. *val* within the body of the binding still resolves to the let-bound local. While *val* is shadowed by a let-bound local, if you want to refer to the var *val*, use its fully qualified name. 2. Is this the desired behavior or will it change in the near future? This behavior is well-established. I have no special knowledge of what may or may not change, but changing this behavior seems very unlikely to me. --Steve -- 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
Re: let-binding overrides binding-binding
but, I can't seem to do this with the 'inc' function: user= (binding [inc (fn [y] (+ 2 y))] (inc 44)) 45 Why doesn't this work? Because inc is inlined, and thus isn't mentioned when your binding occurs. (defn inc Returns a number one greater than num. {:inline (fn [x] `(. clojure.lang.Numbers (inc ~x)))} [x] (. clojure.lang.Numbers (inc x))) Try this: user= (binding [inc (fn [y] (+ 2 y))] (apply inc [44])) 46 -- 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
Re: let-binding overrides binding-binding
On Dec 24, 6:01 pm, Richard Newman holyg...@gmail.com wrote: but, I can't seem to do this with the 'inc' function: user= (binding [inc (fn [y] (+ 2 y))] (inc 44)) 45 Why doesn't this work? Because inc is inlined, and thus isn't mentioned when your binding occurs. Thanks Richardthat's one I hadn't thought of. Try this: user= (binding [inc (fn [y] (+ 2 y))] (apply inc [44])) 46 Actually, I think shadowing core functions is not a great idea but I wanted to understand the principles involved here. Thanks again for your rapid help. cheers, -tom -- 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
let-binding overrides binding-binding
Hi, just found that a binding-form within a let-form does still use the outer value. user (def *val* root binding) #'user/*val* user (defn print-val [] (println *val* is: *val*)) #'user/print-val user (defn let-vs-binding [] (println beginning: *val*) (let [*val* bound by let] (println in LET: *val*) (print-val) (binding [*val* bound by BINDING] (println in BINDING: *val*) (print-val #'user/let-vs-binding user (let-vs-binding) beginning: root binding in LET: bound by let *val* is: root binding in BINDING: bound by let *val* is: bound by BINDING As far as I can see this is the documented behavior. The rules for resolving a symbol described in http://clojure.org/evaluation seem to be the relevant part. Two questions 1. Is my explanation correct? 2. Is this the desired behavior or will it change in the near future? Kind regards, Stefan -- 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
Re: let-binding overrides binding-binding
On Dec 19, 2009, at 5:23 PM, Stefan Kamphausen wrote: 1. Is my explanation correct? It is. The binding form operates on the var, it doesn't affect name resolution within the binding form's body. *val* within the body of the binding still resolves to the let-bound local. While *val* is shadowed by a let-bound local, if you want to refer to the var *val*, use its fully qualified name. 2. Is this the desired behavior or will it change in the near future? This behavior is well-established. I have no special knowledge of what may or may not change, but changing this behavior seems very unlikely to me. --Steve -- 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