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

Q&A's, tips, howto's
Post Reply
nanxiao
Posts: 7
Joined: Mon Jun 25, 2018 1:28 pm

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

Post by nanxiao »

Hi all,

Greeting from me!

When studying define-macro(http://www.newlisp.org/downloads/newlis ... fine-macro), I am a little confused with following statement:
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:

Code: Select all

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

ralph.ronnquist
Posts: 216
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: Why the fexpr's internal variable's name couldn't be sam

Post by ralph.ronnquist »

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):

Code: Select all

(define (mysetx v) (set 'x v))
(define (myset x v) (mysetx v))
You'd get

Code: Select all

> (mysetx "hello")
"hello"
> x
"hello"
> (myset 'x 123)
123
> x
"hello"
Clear as mud :)

nanxiao
Posts: 7
Joined: Mon Jun 25, 2018 1:28 pm

Re: Why the fexpr's internal variable's name couldn't be sam

Post by nanxiao »

Hi Ralph,

Got it! Thanks for your reply!

Best Regards
Nan Xiao

Post Reply