“等于(=)”的区别是什么?和“相同(==)”。在ocaml? [英] What's the difference between "equal (=)" and "identical (==)" in ocaml?

查看:131
本文介绍了“等于(=)”的区别是什么?和“相同(==)”。在ocaml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OCaml 中,我们有两种权益比较

x = y x == y



那么它们之间的确切区别是什么?



在ocaml中是 x = y 就像Java中的 x.equals(y)
$ b x == y 就像 x == y(比较地址) 在Java中?

解决方案

我不知道 x是如何。等于(y)在Java中工作。如果它进行了深入的比较,那么类比就相当接近。有一点需要注意的是,物理平等在OCaml中是一个滑溜的概念(一般而言是泛函)。编译器和运行时系统将会移动值,并且可以随意合并和取消纯(非可变)值。所以你应该只使用 == ,如果你真的知道你在做什么。在某种程度上,它需要熟悉实现(除非必要,这是需要避免的)。



OCaml为创建的具体保证==> 很弱。可变值在物理上与您所期望的相同(即,如果两个变异中的一个实际上会使另一个变异)。但对于不可变的值,唯一的保证是比较物理上相等的值( == )也会相等为( = )。请注意,相反是 not true,因为sepp2k指出了浮动值。



从本质上讲,语言规范告诉你什么 - 可变值是您可以使用 == 作为快速检查来确定两个非可变值是否相等( = )。如果他们比较物理上相等,他们在价值上是平等的。如果他们没有比较物理上的平等,你不知道他们的价值是否相等。您仍然必须使用 = 来决定。

In OCaml, we have two kinds of equity comparisons:

x = y and x == y,

So what's exact the difference between them?

Is that x = y in ocaml just like x.equals(y) in Java?

and x == y just like x == y (comparing the address) in Java?

解决方案

I don't know exactly how x.equals(y) works in Java. If it does a "deep" comparison, then the analogy is pretty close. One thing to be careful of is that physical equality is a slippery concept in OCaml (and functional languages in general). The compiler and runtime system are going to move values around, and may merge and unmerge pure (non-mutable) values at will. So you should only use == if you really know what you're doing. At some level, it requires familiarity with the implementation (which is something to avoid unless necessary).

The specific guarantees that OCaml makes for == are weak. Mutable values compare as physically equal in the way you would expect (i.e., if mutating one of the two will actually mutate the other also). But for non-mutable values, the only guarantee is that values that compare physically equal (==) will also compare as equal (=). Note that the converse is not true, as sepp2k points out for floating values.

In essence, what the language spec is telling you for non-mutable values is that you can use == as a quick check to decide if two non-mutable values are equal (=). If they compare physically equal, they are equal value-wise. If they don't compare physically equal, you don't know if they're equal value-wise. You still have to use = to decide.

这篇关于“等于(=)”的区别是什么?和“相同(==)”。在ocaml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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