!= 在 OCaml 中有意义吗?

发布于 2024-08-04 08:17:29 字数 349 浏览 6 评论 0原文

这似乎是某些类型的等效比较,但不是字符串。

# 3 != 3;;
- : bool = false
# 3 != 2;;
- : bool = true

这正如预期的那样。

# "odp" = "odp";;
- : bool = true
# "odp" != "odp";;
- : bool = true
# "odp" <> "odp";;
- : bool = false

为什么"odp" != "odp" 的计算结果为true?它实际上在做什么?它不应该产生类型错误吗?

It seems to be an equivalency comparison for some types, but not strings.

# 3 != 3;;
- : bool = false
# 3 != 2;;
- : bool = true

This is as expected.

# "odp" = "odp";;
- : bool = true
# "odp" != "odp";;
- : bool = true
# "odp" <> "odp";;
- : bool = false

Why does "odp" != "odp" evaluate to true? What is it actually doing? Shouldn't it generate a type error?

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

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

发布评论

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

评论(5

心碎的声音 2024-08-11 08:17:29

你已经体验过结构平等和身体平等之间的差异。

<> 相当于 =(结构平等),正如 != 相当于 ==(物理平等)

"odg" = "odg"  (* true  *)
"odg" == "odg" (* false *)

是 false,因为每个都在不同的内存位置实例化,这样做:

let v = "odg"
v == v (* true *)
v = v  (* true *)

大多数时候您会想要使用 =<>

编辑结构和物理平等何时等效

您可以使用 what_is_it 函数并找出所有在结构和物理上都相等的类型。正如下面的评论和链接的文章中提到的,字符、整数、单位、空列表和变体类型的某些实例将具有此属性。

you have experienced the difference between structural and physical equality.

<> is to = (structural equality) as != is to == (physical equality)

"odg" = "odg"  (* true  *)
"odg" == "odg" (* false *)

is false because each is instantiated in different memory locations, doing:

let v = "odg"
v == v (* true *)
v = v  (* true *)

Most of the time you'll want to use = and <>.

edit about when structural and physical equality are equivalent:

You can use the what_is_it function and find out all the types that would be equal both structurally and physically. As mentioned in the comments below, and in the linked article, characters, integers, unit, empty list, and some instances of variant types will have this property.

浅听莫相离 2024-08-11 08:17:29

!= 运算符的相反操作符是 == 运算符,而不是 = 运算符。

# "a" != "a" ;;
- : bool = true
# "a" == "a" ;;
- : bool = false

== 运算符是“物理相等”。当您输入 "a" == "a" 时,您会比较两个恰好看起来相似的不同实例字符串,因此运算符返回 false。虽然有一个实例使其返回 true:

# let str = "a"
  in str == str ;;
- : bool = true
# let str = "a"
  in str != str ;;
- : bool = false

The opposite for != operator is == operator, not the = one.

# "a" != "a" ;;
- : bool = true
# "a" == "a" ;;
- : bool = false

The == operator is a "physical equality". When you type "a" == "a", you compare two different instances of strings that happen to look alike, so the operator returns false. While having one instance makes it return true:

# let str = "a"
  in str == str ;;
- : bool = true
# let str = "a"
  in str != str ;;
- : bool = false
红墙和绿瓦 2024-08-11 08:17:29

除了已经提供的所有正确答案之外,还对 OCaml 中的 ==!= 进行了快速解释:

1/ ==!= 公开您确实不想知道的实现细节。示例:

# let x = Some [] ;;
val x : 'a list option = Some []
# let t = Array.create 1 x ;;
val t : '_a list option array = [|Some []|]
# x == t.(0) ;;
- : bool = true

到目前为止,一切顺利:xt.(0) 在物理上是相等的,因为 t.(0) 包含一个指向x 指向的同一块。这是实施的基本知识所规定的。但是:

# let x = 1.125 ;;
val x : float = 1.125
# let t = Array.create 1 x ;;
val t : float array = [|1.125|]
# x == t.(0) ;;
- : bool = false

您在这里看到的是涉及浮点数的其他有用优化的结果。

2/ 另一方面,有一种安全的方法来使用 ==,这是一种快速但不完整的方法来检查结构相等性。

如果您正在二叉树上编写相等函数,

let equal t1 t2 =
  match ...

则检查 t1t2 的物理相等性是检测它们在结构上明显相等的快速方法,甚至无需递归和阅读它们。也就是说:

let equal t1 t2 =
  if t1 == t2
  then true
  else 
    match ...

如果您记住,在 OCaml 中,“布尔或”运算符是“惰性的”,

let equal t1 t1 =
  (t1 == t2) ||
  match ...

A quick explanation about == and != in OCaml in addition to all the correct answers that have already been provided:

1/ == and != expose implementation details that you really don't want to know about. Example:

# let x = Some [] ;;
val x : 'a list option = Some []
# let t = Array.create 1 x ;;
val t : '_a list option array = [|Some []|]
# x == t.(0) ;;
- : bool = true

So far, so good: x and t.(0) are physically equal because t.(0) contains a pointer to the same block that x is pointing to. This is what basic knowledge of the implementation dictates. BUT:

# let x = 1.125 ;;
val x : float = 1.125
# let t = Array.create 1 x ;;
val t : float array = [|1.125|]
# x == t.(0) ;;
- : bool = false

What you are seeing here are the results of an otherwise useful optimization involving floats.

2/ On the other hand, there is a safe way to use ==, and that is as a quick but incomplete way to check for structural equality.

If you are writing an equality function on binary trees

let equal t1 t2 =
  match ...

checking t1 and t2 for physical equality is a quick way to detect that they are obviously structurally equal, without even having to recurse and read them. That is:

let equal t1 t2 =
  if t1 == t2
  then true
  else 
    match ...

And if you keep in mind that in OCaml the “boolean or” operator is “lazy”,

let equal t1 t1 =
  (t1 == t2) ||
  match ...
锦上情书 2024-08-11 08:17:29

他们就像你们班上的两个“汤姆”!因为:

在本例中,"odp" = "odp"
因为它们是具有相同值的两个字符串!!

所以它们不是==,因为它们是两个不同的字符串,存储在不同(内存)位置中,

它们是< code>= 因为它们具有相同的字符串值

更进一步,“odp”是匿名变量。两个匿名变量导致这个 Two 字符串。

为了您的方便:

# "odp" = "odp";; 
- : bool = true 
# "odp" != "odp";; 
- : bool = true 
# "odp" <> "odp";; 
- : bool = false

They are like two "Tom"s in your class! Because:

In this case, "odp" = "odp"
because they are TWO strings with SAME VALUE!!

So they are not == because they are TWO different strings store in different (Memory) location

They are = because they have the identical string value.

One more step deeper, "odp" is anonymous variable. And two anonymous variable leads to this Two strings.

For your convenience:

# "odp" = "odp";; 
- : bool = true 
# "odp" != "odp";; 
- : bool = true 
# "odp" <> "odp";; 
- : bool = false
夏末染殇 2024-08-11 08:17:29

int 是唯一物理和结构相等的类型,因为 int 是唯一未装箱的类型

ints are the only type where physical and structural equality are the same, because ints are the only type that is unboxed

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