[plt-scheme] Re: applying set! on the contents of a variable

From: Majorinc, Kazimir (kazimir at chem.pmf.hr)
Date: Sun Sep 30 16:46:42 EDT 2007

Eli Barzilay wrote:
> On Sep 30, Majorinc, Kazimir wrote:
>   
>> We can at least make local namespaces. Few
>> macros good only to show what's possible. Real usage would
>> require lot of quotes and evals.
>>     
>
> Lots of quotes mean that you should learn how to use quasiquotes, or
> even better -- `define-syntax' and `syntax-rules'.  These will get you
> much smaller and more understandable code. Lots of `eval's are an indication that your solution is not great.
Thanks for explanation of reasons behind non-local eval. What's wrong 
with evals? Do you mean that simpler code is possible or they are just 
generally bad?

;_____________________________________
(define-macro (define# p e)
  `(eval '(define ,p ,e)))

(define-macro (display# p)
  `(display (eval ',p)))

(define-macro (set#!! p e)
  `(eval `(set! ,(eval ',p) ,(eval ',e))))

(define-macro (define-in-namespace . args)
  `(define ,(car args)
     (let ((define-in-namespace-temp
            (make-namespace)))
          ,(append
             `(parameterize ((current-namespace
                              define-in-namespace-temp)))
             (cdr args)))))
;______________________________________
(define# y "start")
(define# x 'y)
(set#!! x "top-level did it.\n")
(display# y)

(define-in-namespace (my-function)
 (define# y "middle")
 (define# x 'y)
 (set#!! x "my-function did it.\n")
 (display# y))

(my-function)
(display# y)
;______________________________________
;top-level did it.
;my-function did it.
;top-level did it.





Posted on the users mailing list.