You are defining 'f', a function that waits for a labeled argument called 'a' and that will send back itself.Please, can someone explain the reason behind the following behaviour: # let f ~a = a;; val f : a:'a -> 'a = <fun>
yes, very confusing. 'f 12' is still not applied (see at the end, it is maybe a problem in ocaml). It is still waiting for the label '~a'. once the label is given, 'f ~a' will be able to give back the result, and then apply it to '12'. So the labelled argument should be a function from an int to something, in order to be applied to '12'. That is what you are doing next :if I apply function f, omiting the label, instead of an error I'll get: # f 12;; - : a:(int -> 'a) -> 'a = <fun>
... a function that accepts a labeled arguments, that is a function from int to 'a, and returns a result of this function: # f 12 ~a:(fun x -> x + 1);; - : int = 13
This is same behaviour : waiting for a labelled argument that will be able to deal with 5 integers.and even more, if I apply it to more unlabled arguments: # f 1 2 3 4 5;; - : a:(int -> int -> int -> int -> int -> 'a) -> 'a = <fun>
It is very confusing...
"As an exception to the above parameter matching rules, if an
application is total (omitting all optional arguments), labels
may be omitted. In practice, many applications are total, so
that labels can often be omitted."
# let f ~x ~y = x - y;; val f : x:int -> y:int -> int = <fun>
# f 3 2;; - : int = 1