Why is the type of a plus ( + ) considered to be int -> int -> int
as opposed to (int * int) -> int
? To me, the second makes sense because it "accepts" a 2-tuple (the addends) and returns a single int
(their sum).
Thank you!
Comparisons (Relational Operators) OCaml distinguishes between structural equality and physical equality (essentially equality of the address of an object). = is structural equality and == is physical equality. Beware: <> is structural not-equals while != is physical not-equals.
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.
There are two equality operators in OCaml, = and == , with corresponding inequality operators <> and !=
You can make a language where (+)
has the type (int * int) -> int
. In fact, SML works exactly this way. It just affects the meaning of infix operators. However OCaml conventions strongly favor the use of curried functions (of the type a -> b -> c
) rather than uncurried ones. One nice result is that you can partially apply them. For example ((+) 7) is a meaningful expression of type int -> int
. I find this notation useful quite often.
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