From: Michael T. <mt...@bb...> - 2006-01-11 16:39:56
|
Hmmm - thanks. I think I'm going to stick to my definition: (define (bound? symbol) (.isDefined symbol)) even though performance isn't much of an issue in the cases I want to use it. I guess the followup question ought to be "How do I unbind a symbol?" but not only is this a hairier problem, but I don't really require an answer... ;-) Cheers, -mik Timothy J Hickey wrote: > Hi mik, > > On Jan 10, 2006, at 3:10 PM, Michael Thome wrote: > >> What is the "proper" way to test to see if a symbol is bound? > > There is no proper way, as far as I can tell. > >> >> I was expecting something like (bound? 'foo). > > If you have created a scheme environment using the jscheme.JScheme > constructor, > then there is an isDefined method which tests whether a symbol is > defined in that interpreter, > e.g. > >> [Timothy-Hickeys-Computer:~/Research/Software/jscheme] tim% java -jar >> lib/jscheme.jar >> JScheme 7.2 (1/10/06 8:55 AM) http://jscheme.sourceforge.net >> > (define js (jscheme.JScheme.)) ;; create a new scheme interpreter >> $1 = jscheme.JScheme@5db13f >> >> > (.eval js '(define x 1)) ;; evaluate a define in that interpreter >> $2 = 1 >> >> > (.isDefined js "x") ;; check to see if the symbol corresponding >> to the string "x" is defined in that interpreter >> $3 = #t >> >> > (.isDefined js "y") ;; notice that "y" is not defined >> $4 = #f >> >> > (.isDefined 'x) ;; also notice that 'x is not defined in the >> toplevel Scheme environment, only in js >> $5 = #f >> >> > (.eval js 'x) ;; we can get the value of 'x in js using eval >> again.... >> $6 = 1 >> > > If you want to check whether a symbol is defined in the toplevel then > (.isDefined 'foo) works fine. > You could also use a macro with tryCatch to test for defined-ness > without having to quote the symbol... > >> > (tryCatch (begin x #t) (lambda(e) #f)) >> $7 = #f >> >> > (define-macro (isDefined s) `(tryCatch (begin ,s #t) (lambda(e) #f))) >> $8 = (macro isDefined (s)...) >> >> > (isDefined sin) >> $9 = #t >> >> > (isDefined sine) >> $10 = #f > > > > I like the try/Catch approach better because the (.isDefined 'x) > relies on the implementation of the Symbol.java class > in the jsint package, which could conceivably change at some point, > whereas the tryCatch approach relies only on > the syntax of macros and tryCatch and the fact that evaluating an > undefined symbol generates an exception.... > But, using (.isDefined 'foo) is ten times faster then the tryCatch > approach ... > >> >> > (time (isDefined foo) 100000) >> $10 = (#f (2929 msec) (367880 bytes)) >> >> > (time (.isDefined 'foo) 100000) >> $11 = (#f (211 msec) (135856 bytes)) > > > Does that help?? > > Best, > ---Tim--- > |