Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected results with OCaml !=

Tags:

equality

ocaml

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?

like image 448
Steve Rowe Avatar asked Jun 16 '10 16:06

Steve Rowe


People also ask

HOW DO YOU DO NOT equal in OCaml?

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.

What is :: In OCaml?

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


1 Answers

!= 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 =).

like image 58
Pascal Cuoq Avatar answered Sep 20 '22 23:09

Pascal Cuoq