Lisp 中 1 和 '1 有什么区别?

发布于 2024-09-04 06:05:42 字数 227 浏览 7 评论 0原文

我从来没有真正想过 Lisp 中的符号是否可以是数字,所以今天我玩了一下:

> '1
1
> (+ '1 '1)
2
> (+ '1 1)
2
> (define a '1)
> (+ a 1)
2

上面的代码是 schema,但在 Common Lisp 和 Clojure 中似乎也大致相同。 1和引用的1有什么区别吗?

I had never really thought about whether a symbol could be a number in Lisp, so I played around with it today:

> '1
1
> (+ '1 '1)
2
> (+ '1 1)
2
> (define a '1)
> (+ a 1)
2

The above code is scheme, but it seems to be roughly the same in Common Lisp and Clojure as well. Is there any difference between 1 and quoted 1?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

待天淡蓝洁白时 2024-09-11 06:05:43

嗯,事实上它们非常不同。然而,'1(quote 1) 完全相同。 (car ''x) 计算结果为符号“quote”。

1 是一个 S 表达式,它是数据(数字 1)的外部表示。要说 1 是“数字对象”或 S 表达式输入该对象都可以接受。通常人们说 1 是实际数字对象的外部表示。

(quote 1) 是另一个 S 表达式,它是一个 list 的 S 表达式,其第一个元素是符号“quote”,第二个元素是数字 1。这就是它已经不同的地方,与函数不同,语法关键字不被视为语言中的对象,并且它们不会评估它们。

然而,两者都是对象(数据)的外部表示,它们评估相同的数据。外部表示为 1 的数字,但是它们肯定不是相同的对象、相同的代码、相同的数据等等,它们只是计算出相同的东西。数字会自行评估。说它们是相同的就是说:

(+ 1 (* 3 3))

并且

(if "Strings are true" (* 5 (- 5 3)) "Strings are not true? This must be a bug!")

是“相同”,它们不是,它们都是不同的程序,只是碰巧终止到相同的值,lisp 形式是也是一个程序,表格是一个数据,它也是一个程序,记住。

另外,我曾经被教过一个方便的技巧,它表明自我评估数据在输入时确实不是符号:

(let ((num 4))
  (symbol? num) ; ====> evaluates to #f
  (symbol? 'num) ; ====> evaluates to #t
  (symbol? '4) ; ====> evaluates to #f
  (symbol? '#\c) ; #f again, et cetera
  (symbol? (car ''x)) ; #t
  (symbol? quote) ; error, in most implementations
)

自我评估数据真正评估自己,它们不是某种“预定义符号”。

Well, they are in fact very different. '1 is however precisely the same as (quote 1). (car ''x) evaluates to the symbol 'quote'.

1 is an S-expression, it's the external representation of a datum, a number 1. To say that 1 is a 'number-object' or an S-expression to enter that object would both be acceptable. Often it is said that 1 is the external representation for the actual number object.

(quote 1) is another S-expression, it's an S-expression for a list whose first element is the symbol 'quote' and whose second element is the number 1. This is where it's already different, syntactic keywords, unlike functions, are not considered objects in the language and they do not evaluate to them.

However, both are external representations of objects (data) which evaluate to the same datum. The number whose external representation is 1, they are however most certainly not the same objects, the same, code, the same datum the same whatever, they just evaluate to the very same thing. Numbers evaluate to themselves. To say that they are the same is to say that:

(+ 1 (* 3 3))

And

(if "Strings are true" (* 5 (- 5 3)) "Strings are not true? This must be a bug!")

Are 'the same', they aren't, they are both different programs which just happen to terminate to the same value, a lisp form is also a program, a form is a datum which is also a program, remember.

Also, I was taught a handy trick once that shows that self-evaluating data are truly not symbols when entered:

(let ((num 4))
  (symbol? num) ; ====> evaluates to #f
  (symbol? 'num) ; ====> evaluates to #t
  (symbol? '4) ; ====> evaluates to #f
  (symbol? '#\c) ; #f again, et cetera
  (symbol? (car ''x)) ; #t
  (symbol? quote) ; error, in most implementations
)

Self evaluating data truly evaluate to themselves, they are not 'predefined symbols' of some sorts.

a√萤火虫的光℡ 2024-09-11 06:05:43

在 Lisp 中,撇号会阻止符号被求值。不禁止在数字前使用撇号,但没有必要,因为数字代表其本身。但是,与任何其他列表一样,它会自动转换为适当的函数调用。解释器认为这些数字与其值一致。

In Lisp, the apostrophe prevents symbols to be evaluated. Using an apostrophe before a number is not forbidden, it is not necessary as the numbers represent themselves. However, like any other list, it automatically gets transformed to an appropriate function call. The interpreter considers these numbers coincide with their value.

内心旳酸楚 2024-09-11 06:05:43

正如已经指出的那样,没有区别,因为数字会自行评估。您可以使用 eval 来确认这一点:

(eval 1)  ;=> 1

顺便说一句,这不仅限于数字。事实上,在 Common Lisp 中,大多数事物都会自行评估。只是除了数字、字符串、符号和列表之外,很少有其他东西被评估。例如,以下作品:

(eval (make-hash-table))  ;equivalent to just (make-hash-table)

As has been pointed out, there is no difference, as numbers evaluate to themselves. You can confirm this by using eval:

(eval 1)  ;=> 1

This is not limited to numbers, by the way. In fact, in Common Lisp, most things evaluate to themselves. It's just that it's very rare for something other than numbers, strings, symbols, and lists to be evaluated. For instance, the following works:

(eval (make-hash-table))  ;equivalent to just (make-hash-table)
无所的.畏惧 2024-09-11 06:05:43

在 Lisp 中,quote 会阻止对后面的表达式求值。 'quote 的简写。因此,'1(quote 1) 相同。

然而,在 Lisp 中,符号永远不可能是数字。我的意思是, 'abc 是一个符号,但 '123 不是(评估为)符号。我认为这是 Lisp 设计的错误。另一种情况是不仅#t#f可以用作布尔表达式。

In Lisp, quote prevent the following expression to be evaluated. ' is a shorthand for quote. As a result, '1 is same as (quote 1).

However, in Lisp, symbols can never be a number. I mean, 'abc is a symbol, but '123 is not (evaluated into) a symbol. I think this is wrong of the design of Lisp. Another case is not only #t or #f can be used as a Boolean expression.

在 Common Lisp 中,'1 是 (QUOTE 1) 的简写。求值时, (QUOTE something) 返回未求值的 something 部分。但是,1 个已评估和 1 个未评估之间没有区别。

所以对读者来说是有区别的:'1 读作 (QUOTE 1) 和 1 读作 1。但是在评估时没有区别。

In Common Lisp, '1 is shorthand for (QUOTE 1). When evaluated, (QUOTE something) returns the something part, unevaluated. However, there is no difference between 1 evaluated and 1 unevaluated.

So there is a difference to the reader: '1 reads as (QUOTE 1) and 1 reads as 1. But there is no difference when evaluted.

や莫失莫忘 2024-09-11 06:05:42

数字是自我评估对象。这就是为什么您不必像处理列表那样担心引用它们。

符号可以由任何字符串组成。如果您想要名称为单个字符1的符号,您可以说:

(intern "1")

打印|1|,建议另一种输入方式:

'|1|

Numbers are self-evaluating objects. That's why you don't have to worry about quoting them, as you do with, say, lists.

A symbol can be made from any string. If you want the symbol whose name is the single character 1, you can say:

(intern "1")

which prints |1|, suggesting an alternate way to enter it:

'|1|
厌味 2024-09-11 06:05:42

引用可以防止表达式稍后被求值。例如,以下内容不是正确的列表:

(1 2 3)

这是因为 Lisp 将 1 解释为函数,但事实并非如此。所以这个列表必须被引用:

'(1 2 3)

当你引用一个非常简单的表达式(比如数字)时,Lisp 实际上不会改变它的行为。

请参阅维基百科:Lisp

Quoting prevents expressions from being evaluated until later. For example, the following is not a proper list:

(1 2 3)

This is because Lisp interprets 1 as a function, which it is not. So the list must be quoted:

'(1 2 3)

When you quote a very simple expression such as a number, Lisp effectively does not alter its behavior.

See Wikipedia: Lisp.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文