Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does != have meaning in OCaml?

Tags:

ocaml

It seems to be an equivalency comparison for some types, but not strings.

# 3 != 3;; - : bool = false # 3 != 2;; - : bool = true 

This is as expected.

# "odp" = "odp";; - : bool = true # "odp" != "odp";; - : bool = true # "odp" <> "odp";; - : bool = false 

Why does "odp" != "odp" evaluate to true? What is it actually doing? Shouldn't it generate a type error?

like image 944
Nick Heiner Avatar asked Sep 11 '09 18:09

Nick Heiner


People also ask

What does :: mean in OCaml?

Regarding the :: symbol - as already mentioned, it is used to create lists from a single element and a list ( 1::[2;3] creates a list [1;2;3] ). It is however worth noting that the symbol can be used in two different ways and it is also interpreted in two different ways by the compiler.

What does in do in OCaml?

As a matter of syntax, in is mostly acting like punctuation; it allows a parser for the language to tell where expr1 stops and expr2 begins. This is especially necessary in ML languages, where putting two expressions next to each other has a specific (and very commonly used) meaning.

What is () in OCaml?

This is a way to indicate there is no value for an argument. It's necessary to do this in ML because all functions are unary. You can't have a function with zero arguments, so instead you pass an argument containing no information, which is () .

What does asterisk mean in OCaml?

The * symbol is used to separate elements of a tuple in data type definitions. The , symbol is used to separate type variables in a parametric type which has more than one variable. E.g., in Student of name * age * class we define a constructor with three arguments.


2 Answers

you have experienced the difference between structural and physical equality.

<> is to = (structural equality) as != is to == (physical equality)

"odg" = "odg"  (* true  *) "odg" == "odg" (* false *) 

is false because each is instantiated in different memory locations, doing:

let v = "odg" v == v (* true *) v = v  (* true *) 

Most of the time you'll want to use = and <>.

edit about when structural and physical equality are equivalent:

You can use the what_is_it function and find out all the types that would be equal both structurally and physically. As mentioned in the comments below, and in the linked article, characters, integers, unit, empty list, and some instances of variant types will have this property.

like image 145
nlucaroni Avatar answered Sep 18 '22 12:09

nlucaroni


The opposite for != operator is == operator, not the = one.

# "a" != "a" ;; - : bool = true # "a" == "a" ;; - : bool = false 

The == operator is a "physical equality". When you type "a" == "a", you compare two different instances of strings that happen to look alike, so the operator returns false. While having one instance makes it return true:

# let str = "a"   in str == str ;; - : bool = true # let str = "a"   in str != str ;; - : bool = false 
like image 27
P Shved Avatar answered Sep 18 '22 12:09

P Shved