How to undefine a variable in Scheme?

2020-02-05 01:50发布

How to undefine a variable in Scheme? Is this possible?

8条回答
萌系小妹纸
2楼-- · 2020-02-05 02:18

In Scheme, variables are defined with either lambda, or one of the various lets. If you want one of them to be 'undefined' then all you need to do is leave the scope that they're in. Of course, that's not really undefining them, it's just that the variable is no longer bound to its previous definition.

If you're making top level definitions, using (define), then technically you're defining a function. Since Scheme is functional, functions never really go away. I suppose that technically, it's stored in some sort of environment function somewhere, so if you were intimately familiar with your implementation (and it's not safeguarded somehow) you could probably overwrite it with your own definition of the globabl environment. Barring that, I'd say that your best bet would be to redefine the function to return the null list- that's really as empty as you get.

查看更多
贪生不怕死
3楼-- · 2020-02-05 02:19

Scheme (R7RS) has no standard compliant way to remove a top-level binding.

If you evaluate a non existing variable, you get an error:

(eval 'a)
; => ERROR: undefined variable: a

If you define it, the variable gets added to the top-level environment.

(define a 1)
(eval 'a)
; => 1

As from now no matter what you do, you will not get an error, if you access the variable.

If you set it to false, you will get false:

(set! a #f)
(eval 'a)
; => #f

Even if you set it to something unspecified, it is unlikely that you get an error:

(set! a (if #f #t))
(eval 'a)
; =>

But Schemes may have a non-standard way to remove a top-level binding. MIT Scheme provides the function unbind-variable.

查看更多
神经病院院长
4楼-- · 2020-02-05 02:19

I think, if your point is to do the equivalent of "free" or de-allocate, then no you're pretty much out of luck. you can't de-allocate a variable. you CAN re-define it to something small, like #f, but once you've done (define foo 'bar) the variable foo will exist in some form until you end the program.

On the other hand, if you use let, or letrec, of course, the name only exists until the relevant close paren...

查看更多
混吃等死
5楼-- · 2020-02-05 02:19

I think your question is not stupid. In AutoLISP has unexisting (undefined) variable apriori supposted value "nil" (even if the variable does not exist in memory - it means - if it is not in a table of variables - then the value is "nil" - "false"). It means also false. And it is also empty list. If you program some kind of list processing function, it is enough to make initial test only by:

(if input-list ....)

When you want to explicitly undefine any variable, you may do this:

(setq old-var nil); or: (setq old-var ())

I like it. The keyword "setq" means "define". What is better on bounding and unbounding variables in other dialects? You must test if they exist, if they are lists, you need garbage-collector, you may not undefine variable to explicitly free memory. Following command can not be written if variable "my-list" is not defined:

(define my-list (cons 2 my-list))

So I think the AutoLISP way is for programming much better. Possibilities, that I written, you may use there. Unfortunately the AutoLISP works in some CAD engineering graphical systems only.

查看更多
兄弟一词,经得起流年.
6楼-- · 2020-02-05 02:20

As stated in the other answers there is no standard way of manipulating the namespace in Scheme. For a specific implementation there might be a solution.

In Racket the top-level variables are stored in a namespace. You can remove a variable using namespace-undefined-variable.

There is no way of removing a local variable.

http://docs.racket-lang.org/reference/Namespaces.html?q=namespace#%28def.%28%28quote.~23~25kernel%29._namespace-undefine-variable%21%29%29

查看更多
做个烂人
7楼-- · 2020-02-05 02:22

You cannot unbind a variable in standard Scheme. You could set! the variable to 'undefined, I guess, or you could write a metainterpreter which reifies environments, allowing you to introduce your own notion of undefining variables.

查看更多
登录 后发表回答