Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml : why comparison operator are type agnostic, whereas arithmetic ones are not?

I am wondering why < operator supports int, string, bool, or float, whereas + only support int.

Could OCaml recognize the underlying arithmetic to use ? What makes comparison operator different from arithmetic ones ? Is it the same for other FP language ?

like image 813
Pierre G. Avatar asked Oct 29 '15 11:10

Pierre G.


People also ask

Which types of operators are used for comparison?

Comparison Operators are used to perform comparisons. Concatenation Operators are used to combine strings. Logical Operators are used to perform logical operations and include AND, OR, or NOT. Boolean Operators include AND, OR, XOR, or NOT and can have one of two values, true or false.

What is == in OCaml?

OCaml distinguishes between structural equality and physical equality (essentially equality of the address of an object). = is structural equality and == is physical equality.

Does OCaml use ==?

There are two equality operators in OCaml, = and == , with corresponding inequality operators <> and !=

What does |> do in OCaml?

The |> operator represents reverse function application.


1 Answers

There is not, at the moment, a notion of "a bit polymorphic" in OCaml (the technical name is "ad-hoc polymorphism"). You can't say "I accept integers and floats but not the rest".

You can say, however "I accept everything", just like the comparison operator (the technical name is "parametric polymorphism"). Do note however than they are lying a bit about that: you can't compare functions, even if the type system can't catch it.

See this answer for details.

like image 83
Drup Avatar answered Oct 25 '22 08:10

Drup