[gambit-list] reading list of symbols defined in top level
Marc Feeley
feeley at iro.umontreal.ca
Sun Feb 1 00:29:03 EST 2009
On 1-Feb-09, at 12:09 AM, lowly coder wrote:
> suppose I have test.scm:
>
> (define (foo ...) ...)
> (define (bar ...) ...)
> (define (yay ...) ...)
>
> (some-magical-funcion) --> '(bar foo yay) # is this possible?
>
> basically, I want to have some function I can call to get a list of
> all symbols defined in the current top level
Depends what you mean by "the current top level". Anyway, the code
below may be close to what you want:
(##namespace ("foo#"))
(##include "~~lib/gambit#.scm")
(define (id x) x)
(define (square x) (* x x))
(##namespace (""))
(define (global-vars-in-namespace ns)
(define (symbol-table->list st)
(apply append
(map (lambda (s)
(let loop ((s s) (lst '()))
(if (symbol? s)
(loop (##vector-ref s 2) (cons s lst))
(reverse lst))))
(vector->list st))))
(define (procedure-in-namespace? s)
(let ((len (string-length ns))
(str (symbol->string s)))
(and (>= (string-length str) len)
(equal? (substring str 0 len) ns)
(let ((val (##global-var-ref (##make-global-var s))))
(procedure? val)))))
(define (keep keep? lst)
(cond ((null? lst) '())
((keep? (car lst)) (cons (car lst) (keep keep? (cdr lst))))
(else (keep keep? (cdr lst)))))
(keep procedure-in-namespace?
(symbol-table->list (##symbol-table))))
(pp (global-vars-in-namespace "foo#")) ;; prints: (foo#square foo#id)
More information about the Gambit-list
mailing list