[plt-scheme] syntax question

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sat Oct 9 09:41:40 EDT 2004

(define-syntax (define-readable-structure stx)
   (syntax-case stx ()
     [(define-readable-structure name ((field reader) ...))
      (let ([prefix-name
             (lambda (pre)
               (datum->syntax-object
                      stx
                      (string->symbol
                       (string-append
                        pre "-"
                        (symbol->string (syntax-e (syntax name)))))))])
      (with-syntax ([read-name (prefix-name "read")]
                    [make-name (prefix-name "make")])
        #`(begin
            (define-struct name (field ...) (make-inspector))
            (define (read-name port)
              (make-name (reader port) ...)))))]))

;; test

(define-readable-structure name1 ((field1 read) (field2 read)))

;; the names exist
make-name1
name1-field1
name1-field2
read-name1

;; the reading works
(let* ([s "1 2"]
        [i (open-input-string s)])
   (equal? (read-name1 i) (make-name1 1 2)))

-- Matthias



Posted on the users mailing list.