From: Michael T. <mt...@bb...> - 2006-01-10 20:10:55
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type"> <title></title> </head> <body bgcolor="#ffffff" text="#000000"> What is the "proper" way to test to see if a symbol is bound?<br> <br> I was expecting something like (bound? 'foo).<br> <br> I've been using (.isDefined 'foo) but surely there is something better...<br> <br> Thanks,<br> mik<br> <div class="moz-signature">-- <br> <table cellpadding="0" cellspacing="0" width="100%"> <tbody> <tr> <td> <font color="#808080" size="2"><b>Michael Thome</b></font><br> <font color="#8f8fff" size="2">BBN Technologies</font> <font color="#808080" size="2">10 Moulton St, Cambridge MA 02138 USA</font><br> <font color="#808080" size="2">phone: +1 617 873 1853</font><br> </td> </tr> </tbody> </table> </div> </body> </html> |
From: Timothy J H. <tjh...@br...> - 2006-01-10 21:43:20
|
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--- |
From: Geoffrey K. <ge...@kn...> - 2006-01-11 09:02:09
|
Good question. MIT/GNU Scheme has: procedure: environment-reference-type environment symbol Returns a symbol describing the reference type of symbol in environment or one of its ancestor environments. The result is one of the following: normal - means symbol is a variable binding with a normal value. unassigned - means symbol is a variable binding with no value. macro - means symbol is a keyword binding. unbound - means symbol has no associated binding. http://www.gnu.org/software/mit-scheme/documentation/scheme_14.html Geoffrey -- Geoffrey S. Knauth | http://knauth.org/gsk On Jan 10, 2006, at 16:42, 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--- |
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--- > |