![]() | ![]() | ![]() | The factorial function | Documentation and user's manual | Table of contents | OCaml programs |
In mathematics, the factorial of a natural number n is the product of all positive integers less than or equal to n, with the convention that the product of no numbers at all is 1. This is written as n! and pronounced n factorial. |
The factorial function is the function that maps any natural number n to n!. |
This is a recursive relation
|
Yet this recursive relation does not give a definition of the factorial function.
To get such a definition a basis relation is also needed. The correct basis relation is
|
From the above basis and recursive relations this Objective Caml recursive definition of the factorial function immediately follows:
|
This code shows that in Objective Caml
f is only locally bound to the functional value.
So, even anonymous functions can be recursively defined. |
Yet a name, for instance fact , can be bound to such a functional value:
|
Consider the evaluation of fact (i) where i is bound to a non-negative value (obeying the specification given by the comment (* n >= 0 *) ).
Look at the sub-expression During the evaluation of This guarantees the termination of the evaluation of Note that the assumption of |
![]() | ![]() | ![]() | The factorial function | Documentation and user's manual | Table of contents | OCaml programs |