eq 之间的区别? 和 = 在方案中?
> (eq? 1 1)
#t
> (eq? 1.1 1.1)
#f
> (= 1.1 1.1)
#t
这是DrScheme 中的交互窗口。 有人可以解释一下 = 和 eq 之间的区别吗? 在计划中?
> (eq? 1 1)
#t
> (eq? 1.1 1.1)
#f
> (= 1.1 1.1)
#t
This is the interaction window in DrScheme. Could somebody please explain the difference between = and eq? in Scheme?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
= 比较数字。 等式? 测试参数是否表示内存中的同一数据对象。 当量? 应该在第二种情况下工作,因为它的测试与 eq 相同? 但专门测试原语。 有关方案中等价谓词的更多信息。
= compares numbers. eq? tests if the parameters represent the same data object in memory. eqv? should work in the second case as it tests same as eq? but tests primitives specially. More on equlivence predicates in scheme here.
我猜从那时起
和
两个 1.1 参数不在内存中驻留在同一位置,并且对于 eq? 返回 #f?
维基百科参考
I would guess that since
and
The two 1.1 arguments do not reside in the same place in memory and return #f for eq?
Wikipedia Reference
eq? 数字是不可预测的。 数字文字是否被埋葬以便相同的数字位于内存中的相同位置,这取决于实现。 例如,Racket 语言最近选择在读取过程中保留此类文字。 http://www.mail-archive.com/[email protected]/msg04893.html
您无法确定您的语言实现的运行时是否会唯一地表示每个数字。 这可能会影响装箱的值,例如浮点数和 bignum。 这就是为什么 = 作为数字谓词存在:它检查内容的相等性,而不是浅指针相等性。
这并不是像Scheme这样的语言所独有的:例如,Python中就出现了相等与相等(is与==)。
eq? on numbers is unpredictable. It's up to the implementation or not whether numeric literals are interred so that the same numbers are in the same location in memory. The Racket language, for example, has recently chosen to intern such literals during reading. http://www.mail-archive.com/[email protected]/msg04893.html
You won't know for sure whether or not your language implementation's runtime will represent each number uniquely. This can affect values that are boxed, like floats and bignums. That's why = exists as a predicate for numbers: it checks for the equal-ness of the content, rather than shallow pointer equality.
This isn't exclusive to languages like Scheme: equality vs equalness occurs in Python (is vs. ==) for example.
第一个区别:
eq?
适用于任意值对,而=
适用于任意数量的数字。还有其他几个等价谓词 。 它们中的大多数只接受两个参数。
=
在 “数字”章节first difference:
eq?
works with any pair of values, while=
works with any number of numbers.there are several other equivalence predicates. Most of them only accept exactly two parameters.
=
is defined in the 'numbers' chapter