2010-06-16 26 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