OCaml!=的意外结果

Ste*_*owe 16 ocaml equality

据我所知,=和!=应该用于OCaml中的字符串.我看到了奇怪的结果,虽然我想更好地理解.

当我用=比较两个字符串时,我得到了我期望的结果:

# "steve" = "steve";;
- : bool = true
# "steve" = "rowe";;
- : bool = false
Run Code Online (Sandbox Code Playgroud)

但是当我尝试的时候!=我没有:

# "steve" != "rowe";;
- : bool = true
# "steve" != "steve";; (* unexpected - shouldn't this be false? *)
- : bool = true
Run Code Online (Sandbox Code Playgroud)

谁能解释一下?有一个更好的方法吗?

Pas*_*uoq 19

!=不是否定=.<>=你应该使用的否定:

# "steve" <> "rowe" ;;
- : bool = true
# "steve" <> "steve" ;;
- : bool = false
# 
Run Code Online (Sandbox Code Playgroud)

!=是否是否定==,如果你是OCaml初学者,你不应该使用这两者中的任何一个.它们可能有点棘手,并且它们被正式指定(唯一的保证是如果==它们是两个值=).

  • 前一个问题涵盖了一些细微之处.http://stackoverflow.com/questions/1412668/does-have-meaning-in-ocaml (2认同)