我是否缺少有关在LISP中嵌入符号的一些重要事实? [英] Am I missing some important fact about interning symbols in LISP?

查看:71
本文介绍了我是否缺少有关在LISP中嵌入符号的一些重要事实?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单点.这是我几次尝试并在剪辑中使用符号的方法.

To be brief. Here is my several tries to intern and use a symbol in clisp.

 [1]> (setq sym (intern "foo"))
 |foo|
 [2]> (eq sym 'foo)
 NIL

为什么?

 [3]> (defun internup (me &optional (package *package*))
   (intern (string-upcase me) package))
 INTERNUP

 [4]> (eq 'abc (internup "abc"))
 T

可能必须大写.

 [12]>(let ((abc 2))
   (eval '(+ 2 abc)))

 *** - EVAL: variable ABC has no value
 The following restarts are available:

 [18]> (let ((abc 2))
 (eval '(+ 2 'abc)))

 *** - +: ABC is not a number
 The following restarts are available:

有趣.我应该事先设置吗?

Interesting. Should I set it before.

 [14]> (setq a (internup "abc"))
 ABC

 [15]> (let ((abc 2))
 (eval '(+ 2 a)))

 *** - +: ABC is not a number
 The following restarts are available:

又一次错了.嗯,我一定想念有关LISP中的符号间的一些重要事实.你能帮我吗?

And again wrong. Hmm I must be missing some important fact about interning symbols in LISP. Can you help me ?

推荐答案

您的问题与实习无关.

第一个问题确实是由于读者总是将符号大写,因此您需要调用(实习生"FOO")以获得与'foo相同的结果.

The first problem is indeed causes by the fact that the reader will always uppercase the symbols, so you need to call (intern "FOO") in order to get the same result as 'foo.

EVAL的问题是由于LET引入了在EVAL内部不可见的词法绑定的事实引起的.如果您真的想让它正常工作,则必须声明abc是特殊的,就像这样:

The problem with EVAL is caused by the fact that LET introduces a lexical binding which is not visible inside EVAL. If you really want this to work you'd have to declare abc to be special, like so:

(let ((abc 2))
    (declare (special abc))
    (eval '(1+ abc)))

特殊声明将使变量具有动态绑定,而不是词法绑定(后者意味着绑定仅限于本地词法上下文,即在LET形式内.使用特殊声明,变量可用于任何对象从该表格中调用).

A special declaration will cause the variable to have dynamic binding, instead of lexical binding (the latter means that the binding is limited to the local lexical context, i.e. within the LET form. With the special declaration the variable is available to anything that is called by from that form).

请注意,同时使用特殊声明和eval是您应该非常注意的事情,并且您可能首先应该重新考虑对EVAL的使用.实际上很少需要使用它.在大多数情况下,您实际上是在寻找使用lambda函数.

Note that the use of both special declarations and eval is something you should be very careful about and you should probably rethink your use of EVAL in the first place. It is very rare that you actually need to use it. In most cases you're actually looking for the use of lambda functions instead.

这篇关于我是否缺少有关在LISP中嵌入符号的一些重要事实?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆