OCaml type for plus operator

Why is the plus type (+) considered int -> int -> int , and not (int * int) -> int ? For me, the second meaning is because it "takes" a 2-tuple (adds) and returns a single int (their sum).

Thanks!

+5
source share
2 answers

You can create a language in which (+) is of type (int * int) -> int . In fact, SML does just that. It just affects the meaning of infix operators. However, OCaml conventions strongly support the use of curry functions (such as a -> b -> c ) rather than non-oriented ones. A good result is that you can partially apply them. For example ((+) 7) is a meaningful expression of type int -> int . I find this note useful quite often.

+11
source

This may seem a little useless, but this is because the function takes two arguments.

When a function takes a tuple, it actually takes one argument.

Since (+) is a built-in function, using one argument will not be useful, since it will look like + (1,2) , not 1 + 2 .

+2
source

Source: https://habr.com/ru/post/919541/


All Articles