What's the difference between “equal (=)” and “identical (==)” in ocaml?

后端 未结 4 908
慢半拍i
慢半拍i 2020-12-08 10:10

In OCaml, we have two kinds of equity comparisons:

x = y and x == y,

So what\'s exact the difference betw

4条回答
  •  半阙折子戏
    2020-12-08 10:43

    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?

    Yes, that's it. Except that in OCaml you can use = on every kind of value, whereas in Java you can't use equals on primitive types. Another difference is that floating point numbers in OCaml are reference types, so you shouldn't compare them using == (not that it's generally a good idea to compare floating point numbers directly for equality anyway).

    So in summary, you basically should always be using = to compare any kind of values.

提交回复
热议问题