[plt-scheme] (require... ) expanded from a macro

From: Dan Muresan (danmbox at gmail.com)
Date: Sun Oct 22 00:58:12 EDT 2006

>    (define-syntax (require-extension stx)
>      (syntax-case stx (srfi)
>        ((_ (srfi n))
>         (number? (syntax-e #'n))
>         (with-syntax ([path (datum->syntax-object
>                              #'n
>                              `(lib ,(format "~a.ss" (syntax-e #'n))
>                                    "srfi"))])
>            #'(require path)))))

I tried to create a similar macro, (require-library), which
(include)'s a file only once, but it doesn't seem to work:

(define-syntax require-library
  (lambda (stx)
    (define libraries '())  ;; broken, should be outside lambda
    (syntax-case stx (quote)
      ((_ (quote lib))
       (let ((slib (syntax-object->datum #'lib)))
         (if (memv slib libraries)
             (syntax (begin))
             (let ((str (string-append
                           (symbol->string
                             (syntax-e #'lib)) ".scm")))
               (set! libraries (cons slib libraries))
               (display (format "Including ~a\n" slib))
               (with-syntax ((fname (datum->syntax-object #'lib str)))
                 #'(include fname)))))))))

(require-library 'x)  ;; should expand to (include "x.scm")
;; x.scm just contains (define (f x) x)

f  ;; raises "undefined identifier"

The macro runs, but the binding for f seems to be hidden.

Firstly, why are the included bindings hidden? And, secondly, where
should I put the "libraries" catalog (inside (lambda (stx) ...) is
obviously broken, but putting them outside generates an error)?

-- Dan


Posted on the users mailing list.