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

Q&A's, tips, howto's

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

Postby nanxiao » Wed Jul 11, 2018 10:26 am

Hi all,

Greeting from me!

When studying define-macro(http://www.newlisp.org/downloads/newlisp_manual.html#define-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
nanxiao
 
Posts: 7
Joined: Mon Jun 25, 2018 1:28 pm

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

Postby ralph.ronnquist » Sun Jul 15, 2018 12:07 am

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 :)
ralph.ronnquist
 
Posts: 176
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

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

Postby nanxiao » Mon Jul 16, 2018 5:29 am

Hi Ralph,

Got it! Thanks for your reply!

Best Regards
Nan Xiao
nanxiao
 
Posts: 7
Joined: Mon Jun 25, 2018 1:28 pm


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 4 guests