[plt-scheme] redefining set!

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Mon Jun 18 16:35:30 EDT 2007

Hans Oesterholt-Dijkema skrev:
> I'm trying to redefine set! for variables part of a closure,
> when within the closure. For variables outside the closure,
> the behaviour should be different.
> 
> I think the method you proposed will fit the need. Going to
> try it out in a spike.

Since your scope is "inside the closure" the let-syntax
example in the documentation is close to home.
Here is an example that the automatically doubles
all values in the rhs of a set! inside a double-lambda.

(module double mzscheme
   (provide foo)

   (define-syntax (double-lambda stx)
     (syntax-case stx ()
       [(double-lambda (var ...) expr ...)
        (syntax/loc stx
          (lambda (var ...)
            (let-syntax ([var (make-set!-transformer
                               (lambda (so)
                                 (syntax-case so (set!)
                                   [(set! id e)
                                    (identifier? #'id)
                                    (syntax (set! var (* 2 e)))]
                                   [id
                                    (identifier? #'id)
                                    #'var])))]
                         ...)
              expr  ...)))]))

   (define foo
     (double-lambda (x y)
                    (set! x 1)
                    (set! y 3)
                    (list x y))))


(require double)
(foo 42 42)  ; => 2 6

-- 
Jens Axel Søgaard



Posted on the users mailing list.