2010-06-16 27 views
16

从我所知道的,=和!=应该在OCaml中的字符串上工作。虽然我希望更好地理解,但我看到了奇怪的结果。OCaml意外的结果!=

当我比较两个字符串=我得到的结果我想到:

# "steve" = "steve";; 
- : bool = true 
# "steve" = "rowe";; 
- : bool = false 

但是当我尝试=我不行!

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

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

回答

19

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

# "steve" <> "rowe" ;; 
- : bool = true 
# "steve" <> "steve" ;; 
- : bool = false 
# 

!===否定的,如果你是一个初学者ocaml的,你不应该使用任何这两个呢。他们可能有点棘手,并且他们正式地被低估(唯一的保证是,如果两个值是==他们是=)。

+2

前一个问题涵盖了一些微妙之处。 http://stackoverflow.com/questions/1412668/does-have-meaning-in-ocaml – nlucaroni 2010-06-17 22:05:03