Variable name clashes

Q&A's, tips, howto's
Locked
jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Variable name clashes

Post by jopython »

Code: Select all

> (module "macro.lsp")
MAIN
> 
(macro (square-sum X Y)
   (letn ((First X) 
          (Second Y)
          (Sum (+ First Second)))
      (* Sum Sum)))

(lambda-macro (X Y) (expand '(letn ((First X) (Second Y) (Sum (+ First Second))) 
   (* Sum Sum))))

> (let ((First 9)) (square-sum 1 First))
4
> 

Instead of getting a 100 , i get a 4.
How do newlispers solve this issue?

johu
Posts: 143
Joined: Mon Feb 08, 2010 8:47 am

Re: Variable name clashes

Post by johu »

Code: Select all

(macro (square-sum First Second)
   (let ((Sum (+ First Second)))
      (* Sum Sum)))
or

Code: Select all

(macro (square-sum X Y)
  (pow (+ X Y) 2))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Variable name clashes

Post by Lutz »

You have a clash between "First" on the caller level and "First" inside a 'define-macro statement generated by 'macro.

You could put 'square-sum in its own namespace:

Code: Select all

(module "macro.lsp")

(context 'mymacros)

(macro (square-sum X Y)
   (letn ((First X)
          (Second Y)
          (sum (+ First Second)))
      (* sum sum)))

(context MAIN)

(let ((First 9)) (mymacros:square-sum 1 First)) => 100
Unfortunately a default functor as used in one of the examples in http://www.newlisp.org/downloads/newlis ... fine-macro will not work together with the macro.lsp module. UPDATE: a new macro.lsp, now online, also allows default functors.

On a general note:
Use expansion macros generated using the macro.lsp only for small one-liners. The function/fexpr call overhead saved when using macro.lsp is not worth it with any code longer than a line.

Almost always using 'define-macro with 'letex or 'evel is the better choice. Fexprs generated with 'define-macro work well as context default functions and load faster when newLISP is reading source.

jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Re: Variable name clashes

Post by jopython »

Thanks for the reply. I should have worded my question appropriately.

How to use variables/symbols in macros, so that in future these don't interfere with the user defined variables? In these case it is the 'First' and 'Second' variables.

jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Re: Variable name clashes

Post by jopython »

Lutz,

Thank you for the 'context' solution.
I also accept your advice for using macros.lsp for one liners only in future.

jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Re: Variable name clashes

Post by jopython »

Lutz,
The NEW macro.lsp has the same variable clash issue when I use default functors.

Code: Select all

newLISP v.10.4.5 on Linux IPv4/6 UTF-8 libffi, execute 'newlisp -h' for more info.

> (load "http://www.newlisp.org/code/modules/macro.lsp")

MAIN
> 
> 
(macro (square-sum:square-sum X Y)
   (letn ((First X)
          (Second Y)
          (sum (+ First Second)))
      (* sum sum)))

(lambda-macro (X Y) (expand '(letn ((First X) (Second Y) (sum (+ First Second))) 
   (* sum sum))))
> (context MAIN)
MAIN
> 
> (define (callsq a b) (square-sum a b))
(lambda (a b) 
 (letn ((First a) (Second b) (sum (+ First Second))) 
  (* sum sum)))
> (square-sum 1 9)
100
> (let (First 9) (square-sum 1 First))
4

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Variable name clashes

Post by Lutz »

the whole definition must go into the 'square-sum name space:

Code: Select all

newLISP v.10.4.5 on OSX IPv4/6 UTF-8 libffi, execute 'newlisp -h' for more info.

> 
(load "http://www.newlisp.org/code/modules/macro.lsp")
(context 'square-sum)
(macro (square-sum:square-sum X Y)
   (letn ((First X)
          (Second Y)
          (sum (+ First Second)))
      (* sum sum)))
(context MAIN)

MAIN
square-sum
(lambda-macro (X Y) (expand '(letn ((First X) (Second Y) (sum (+ First Second))) 
   (* sum sum))))
MAIN
> (let (First 9) (square-sum 1 First))
100
>
now all variables used in the definition are protected.

Locked