[plt-scheme] create name with syntax-case

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Tue Jun 5 09:39:40 EDT 2007

Voilà, with pedagogic identifiers for the intermediate stages:

(define-syntax define-accessor
   (lambda (x)
     (syntax-case x ()
       ((_ field position)
        (let* ([field-as-syntax (syntax field)]
               [field-as-symbol (syntax-e field-as-syntax)]
               [field-as-string (symbol->string field-as-symbol)]
               [name-as-string  (string-append "implementation->"  
field-as-string)]
               [name-as-symbol  (string->symbol name-as-string)]
               [implementation->field (datum->syntax-object x name-as- 
symbol)])
          #`(define (#,implementation->field implementation)
              (let ((entry (hash-table-ref/default database  
implementation #f)))
                (if entry
                    (vector-ref entry position)
                    (error "no such implementation known!")))))))))

(define-accessor eval-command 0)
(define-accessor interpret-command 1)
(define-accessor compile-command 2)
(define-accessor run-compiled-command 3)

I have a library that does this kind of transformation for me. You  
might find it on planet too. -- Matthias






On Jun 5, 2007, at 8:41 AM, Marijn Schouten (hkBst) wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi list,
>
> I would like to know how I should change the following snippet such  
> that it
> will define the 4 functions implementation->eval-command
> implementation->interpret-command implementation->compile-command and
> implementation->run-compiled-command.
>
> (define-syntax define-accessor
>   (lambda (x)
>     (syntax-case ()
>         ((_ field position)
>          (define (implementation->field implementation)
>            (let ((entry (hash-table-ref/default database  
> implementation #f)))
>              (if entry (vector-ref entry position) (error "no such
> implementation known!"))))))))
>
> (define-accessor eval-command 0)
> (define-accessor interpret-command 1)
> (define-accessor compile-command 2)
> (define-accessor run-compiled-command 3)
>
> Marijn
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v2.0.4 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iD8DBQFGZVn6p/VmCx0OL2wRAjeiAJ91QPwQX29cCX2T3L1Ogm+D9+L7PQCfd55D
> WUjrfQN974/IN/eI98MdRbw=
> =Xeb7
> -----END PGP SIGNATURE-----
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.