Hello, dear friends!
I am VERY new to Ocaml and struggle through its syntax.
Here are two functions:
let rec fact n = if n < 2 then 1 else n * fact(n-1) ; ;
val fact : int → int =
let succ x = x+1 ; ;
val succ : int → int =
That is easy: int in , int out.
But there is more:
let compose f g x = f(g x) ; ;
val compose : (’a → ’b) → (’c → ’a) → ’c → ’b =
compose fact succ 8 ; ;
- : int = 362880
I read it like this: "Function “compose” receives three arguments: two functions “f” and “g” and an integer(or any numeric type) “x”.
First we apply “succ” to x and then apply “fact” to the result of that application.
So, we go from left to right, because it is application and it is right associated.
When it comes to the type of the function provided by the REPL, we go the other direction, right to left, and I read it like this:
('a → 'b) is the first function applied, in this case “succ”. It recives “a” and returns (b). We keep them in parenthesis to show that is one function.
('c → 'a) is the second function, “fact”, applied to whatever ('a ->'b) passes on to it. It receives type c and produces an “a”, again? And “c” is what “succ” passed to it?
Then somehow whatever “fact” produced becomes a “c” and is returnd as “b”.
So, as you can see, i am totally lost when it comes to deciphering that function’s type (i mean “compose”).
Could somebody be so kind and LITERALLY take apart this type definition: and explain the flow of information and why a, b, c, a, c, and b are used and in this particular order?
Thanks a lot in advance!!!