Why the fexpr's internal variable's name couldn't be same as

Started by nanxiao, July 11, 2018, 03:26:34 AM

Previous topic - Next topic

nanxiao

Hi all,



Greeting from me!



When studying define-macro(http://www.newlisp.org/downloads/newlisp_manual.html#define-macro">//http://www.newlisp.org/downloads/newlisp_manual.html#define-macro), I am a little confused with following statement:
Quote
Note that in fexprs, the danger exists of passing a parameter with the same variable name as used in the define-macro definition. In this case, the fexpr's internal variable would end up receiving nil instead of the intended value:


;; not a good definition!

(define-macro (my-setq x y) (set x (eval y)))  

;; symbol name clash for x

(my-setq x 123)  → 123
x                → nil


Could anyone elaborate why the fexpr's internal variable's name couldn't be same as parameter's name?



Thanks very much in advance!



Best Regards

Nan Xiao

rrq

Isn't it just a normal effect of dynamic binding; that any assignment always affects the "inner-most" variable of the name. It's not special to fexprs, but you can confuse yourself quite easily with "normal" lambdas if/when a lambda body refers to a variable outside its scope.



For example, if you have the following definitions (well, you wouldn't, but you can imagine it):
(define (mysetx v) (set 'x v))
(define (myset x v) (mysetx v))
You'd get > (mysetx "hello")
"hello"
> x
"hello"
> (myset 'x 123)
123
> x
"hello"

Clear as mud :)

nanxiao

Hi Ralph,



Got it! Thanks for your reply!



Best Regards

Nan Xiao