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?
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.
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.
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 () .
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.
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.
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
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