isBound? predicate in scheme

74 Views Asked by At

Does anyone have a suggestion on how i can check if variable x is bound or not?

I want to differ between unbound variables and symbols for example but the symbol? predicate is not good here because (symbol? x) give me an error.

i deal only with unbound variables!

i'll give you an example:

(pattern-rule
 `(car ,(?'expr))
    (lambda (expr) `,(car (fold expr))))

this code is part of a folder procedure which is part of a parser.

the returned evaluation on (fold '(car (cons '1 '2))) is '1

the returned evaluation on (fold '(car x)) should be (car x) (i mean, the string (car x))

but i can't figure out how to do this part!

1

There are 1 best solutions below

0
On

I understand that you are writing your own parser? If so, you need to have an explicit representation of the environment. Each time you encounter a binding construct such as lambda or let, you add the bound variables to the environment. When you need to found out if a variable is bound or not, you look it up in the environment - if it is present, then it is bound, if not it is undbound.