How to check if a variable exists in a schema?

Is there a way to check if a variable exists in a schema? Even things like (if the variable) or (the variable is null?) Cause errors because the variable is not defined. Is there any function that returns whether or not a variable exists?

+6
lisp scheme
source share
4 answers

Here is an example in Racket:

#lang racket (define x 1) (define-namespace-anchor ns) (define (is-bound? nm) (define r (gensym)) (not (eq? r (namespace-variable-value nm #t (lambda () r) (namespace-anchor->namespace ns))))) (is-bound? 'x) (is-bound? 'not-bound-here) 
+3
source share

You want to ask questions about the environment. This is not possible with R5RS, and I'm not sure about R6RS. Of course, I would like to do this using only the Scheme standard (and this may be part of R7RS - look for "Environment queries" in the list of elements that they are likely to work on ).

As far as I can tell, at present there are only special solutions, so you will have to read your implementation documentation.

The chicken supports that with the oblist egg (it allows you to get a list of all interned characters), as well as with the egg environments , which lets you ask if one character is connected.

Depending on your implementation, if it may be possible to verify this by making a reference to the variable and catching the exception, check to see if it was an exception, or something similar to this.

+3
source share

According to R6RS, this is a violation of the syntax for making a call to an unbound variable.

http://www.r6rs.org/final/html/r6rs/r6rs-ZH-12.html#node_sec_9.1

However, depending on your implementation, there should be a way (theoretically, at least) to query the environment and check if the variable is a member. However, you need to do something else to read.

http://www.r6rs.org/final/html/r6rs-lib/r6rs-lib-ZH-17.html#node_idx_1268

+2
source share

This feature is built into the Mit-Scheme.

 #lang scheme (define x "hello world") (environment-bound? (nearest-repl/environment) 'x) (environment-bound? (nearest-repl/environment) 'not-x) 
+2
source share

All Articles