Syntax error with function definition and type annotation

Compilation of program below produces a syntax error. Why ?

let rec (fact:int->int) =fun (n:int) ->  
    (if n=0 then 1
    else n*fact (n-1)):int
in
print_int (fact 4);;

Hello @devosalain,

is not a valid expression: see the syntax for expressions in OCaml here and there.

Thus, here, one should write instead:

(if n = 0 then 1 else n * fact (n - 1) : int)

to add a type annotation on the expression if n = 0 then 1 else n * fact (n - 1).

By the way, note that the function fact you define is overly type-annotated. The annotations on the parameter n of the function fact and on the body of fact are redundant with the one on fact itself.

2 Likes