[plt-scheme] redefining set!

From: Hans Oesterholt-Dijkema (hdnews at gawab.com)
Date: Mon Jun 18 17:16:44 EDT 2007

I didn't say it quite right. I'd really like to do something
like this:

(define-syntax def-class
  (syntax-rules ()
    ((_ (f e1 ...) (member1 expr1) ...)
     (begin
       (define (f e1 ...)
         (define member1 expr1)
         ...

         (define-syntax obj
           (syntax-rules ()
             ((_ member1 expr) (set! member1 expr))
             ...))

         (syntax obj))))))

(def-class (cl a) (m (* a a)))
(define x (cl 5))
(-> x m 8)

I'd like somehow to (-> x m 8) to set the member m to 8,
using syntax expansion. I'm however not sure how to reach
that.

--Hans


Jens Axel Søgaard schreef:
> 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
>



Posted on the users mailing list.