!=在OCaml中有含义吗? [英] Does != have meaning in OCaml?

查看:151
本文介绍了!=在OCaml中有含义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

这是预期的.

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

为什么"odp" != "odp"评估为true?它实际上在做什么?它不应该产生类型错误吗?

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

推荐答案

您已经体验到结构平等和物理平等之间的区别.

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 <>.

编辑何时等同于结构和物理:

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

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.

这篇关于!=在OCaml中有含义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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