How do you check to see if a symbol is already bound? To be used in this fashion:
(if (bound? x) (set! x 22) (define x 22))
Afficher les réponses par date
What about this?
(define-macro (bound? x) `(with-exception-handler (lambda (e) #f) (lambda () (and ,x #t))))
(pp (bound? x)) (let ((y 3)) (pp (bound? y)))
Cheers,
P!
2011/5/9 William James w_a_x_man@yahoo.com:
How do you check to see if a symbol is already bound? To be used in this fashion:
(if (bound? x) (set! x 22) (define x 22)) _______________________________________________ Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
Adrien - in addition to what you suggested he wants to define something in case it's unbound.
What about, based on your macro
(define-macro (define/only-if-unbound var-name value) (if (not (symbol? var-name)) (error "Symbol expected")) `(define ,var-name (with-exception-catcher (lambda (e) ,value) (lambda () ,var-name))))
(define xyz 1) (define/only-if-unbound xyz 2) xyz
1
(define/only-if-unbound xyz2 123) (define/only-if-unbound xyz2 456) xyz2
123
Only use this kind of coding strategy if you have a really good reason to.
Mikael
2011/5/9 Adrien Piérard pierarda@iro.umontreal.ca
What about this?
(define-macro (bound? x) `(with-exception-handler (lambda (e) #f) (lambda () (and ,x #t))))
(pp (bound? x)) (let ((y 3)) (pp (bound? y)))
Cheers,
P!
2011/5/9 William James w_a_x_man@yahoo.com:
How do you check to see if a symbol is already bound? To be used in this fashion:
(if (bound? x) (set! x 22) (define x 22)) _______________________________________________ Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
-- Français, English, 日本語, 한국어 _______________________________________________ Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
On 2011-05-08, at 4:30 PM, William James wrote:
How do you check to see if a symbol is already bound? To be used in this fashion:
(if (bound? x) (set! x 22) (define x 22))
This is the official way to test is a global variable is bound:
(define (bound? name) (not (##unbound? (##global-var-ref (##make-global-var name)))))
Marc
On Mon, May 9, 2011 at 8:55 AM, Marc Feeley feeley@iro.umontreal.ca wrote:
On 2011-05-08, at 4:30 PM, William James wrote:
How do you check to see if a symbol is already bound? To be used in this fashion:
(if (bound? x) (set! x 22) (define x 22))
This is the official way to test is a global variable is bound:
(define (bound? name) (not (##unbound? (##global-var-ref (##make-global-var name)))))
Marc
I tried this running this in the interpreter, and got the following result:
(define (bound? name)
(not (##unbound? (##global-var-ref (##make-global-var name)))))
(bound? mytest)
*** ERROR IN (console)@1779.9 -- Unbound variable: mytest 1>
Am I missing something? 'mytest' was undefined, obviously, but shouldn't bound? have returned #f ?
On 2011-05-20, at 10:40 AM, REPLeffect wrote:
On Mon, May 9, 2011 at 8:55 AM, Marc Feeley feeley@iro.umontreal.ca wrote:
On 2011-05-08, at 4:30 PM, William James wrote:
How do you check to see if a symbol is already bound? To be used in this fashion:
(if (bound? x) (set! x 22) (define x 22))
This is the official way to test is a global variable is bound:
(define (bound? name) (not (##unbound? (##global-var-ref (##make-global-var name)))))
Marc
I tried this running this in the interpreter, and got the following result:
(define (bound? name)
(not (##unbound? (##global-var-ref (##make-global-var name)))))
(bound? mytest)
*** ERROR IN (console)@1779.9 -- Unbound variable: mytest 1>
Am I missing something? 'mytest' was undefined, obviously, but shouldn't bound? have returned #f ?
Try
(bound? 'mytest)
Marc
On Fri, May 20, 2011 at 9:52 AM, Marc Feeley feeley@iro.umontreal.ca wrote:
On 2011-05-20, at 10:40 AM, REPLeffect wrote:
On Mon, May 9, 2011 at 8:55 AM, Marc Feeley feeley@iro.umontreal.ca wrote:
On 2011-05-08, at 4:30 PM, William James wrote:
How do you check to see if a symbol is already bound? To be used in this fashion:
(if (bound? x) (set! x 22) (define x 22))
This is the official way to test is a global variable is bound:
(define (bound? name) (not (##unbound? (##global-var-ref (##make-global-var name)))))
Marc
I tried this running this in the interpreter, and got the following result:
(define (bound? name)
(not (##unbound? (##global-var-ref (##make-global-var name)))))
(bound? mytest)
*** ERROR IN (console)@1779.9 -- Unbound variable: mytest 1>
Am I missing something? 'mytest' was undefined, obviously, but shouldn't bound? have returned #f ?
Try
(bound? 'mytest)
Marc
oh, duh. Now I feel really stupid. :-D
I advise you to go back to your intent that leads you to want to do this, and revise what you do to fulfill it.
Probably you can achieve what you thought you'd do this way, some other way that's much better. (Better in the sense that you don't need to make scope, module or Scheme system-specific hacks to make it go around.) Not keeping state in globals for instance, can be part of the solution.
2011/5/8 William James w_a_x_man@yahoo.com
How do you check to see if a symbol is already bound? To be used in this fashion:
(if (bound? x) (set! x 22) (define x 22)) _______________________________________________ Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list