[plt-scheme] verifying whether a symbol is defined at compile time?

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Sun Apr 22 07:03:03 EDT 2007

Hi Yin-So,

Matthew's hint about identifier-binding is probably what you need now.
In more general situations you can keep track of the names (and/or 
associated informations) by a define-like macro with the help
of a little bookkeeping.

(module names mzscheme
   (provide register-name defined?)

   (require (prefix srfi: (lib "1.ss" "srfi")))

   (define defined-names '())

   (define (defined? name)
     (srfi:member name defined-names module-identifier=?))

   (define (register-name name)
     (cond [(defined? name)
            (raise-syntax-error
               'define-once
                format "the name '~a' is already defined"
                       (syntax-e name))
                name)]
           [else
            (set! defined-names (cons name defined-names))])))

(module define-once mzscheme
   (provide define-once)
   (require-for-syntax names)

   (define-syntax (define-once stx)
     (syntax-case stx ()
       [(define-once name expr)
        (register-name #'name)
        #'(begin
            (require names)
            (define name expr))])))

(module test mzscheme
   (provide foo bar)
   (require define-once)
   (define-once foo 1)
   (define-once bar 2)
   ;(define-once bar 3)
   )

(require test)
foo
bar


-- 
Jens Axel Søgaard



Posted on the users mailing list.