From what I can tell, = and != is supposed to work on strings in OCaml. I'm seeing strange results though which I would like to understand better.
When I compare two strings with = I get the results I expect:
# "steve" = "steve";;
- : bool = true
# "steve" = "rowe";;
- : bool = false
but when I try != I do not:
# "steve" != "rowe";;
- : bool = true
# "steve" != "steve";; (* unexpected - shouldn't this be false? *)
- : bool = true
Can anyone explain? Is there a better way to do this?
OCaml provides the following equality and comparison operators: = (equal), <> (not equal), and the obvious < , > , <= , >= . Unlike arithmetic operators, they do work with values of any type, but those values must be of the same type.
:: means 2 camel humps, ' means 1 hump! – Nick Craver. Feb 27, 2010 at 12:09. Ok a decent comment: merd.sourceforge.net/pixel/language-study/… I don't use oCaml, but there's a full syntax list you my find useful, even if there's no real context to it.
!=
is not the negation of =
. <>
is the negation of =
that you should use:
# "steve" <> "rowe" ;;
- : bool = true
# "steve" <> "steve" ;;
- : bool = false
#
!=
is the negation of ==
, and if you are an OCaml beginner, you should not be using any of these two yet. They can be a little tricky, and they are officially underspecified (the only guarantee is that if two values are ==
they are =
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With