This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

Factorial function
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2007-01-23 (20:12) From: Jonathan Roewen Subject: Re: [Caml-list] Factorial function
```Recursive call to fact is not in tail position. And by overflow, you
mean stack overflow.

If fact was in tail position, it would loop forever (since there is no
base case).

You can observe that fact is indeed not in tail position be rewriting
the expression without the infix multiplication.

( * ) n (fact (n-1)

or:
let mul x y = x * y => mul n (fact (n-1))

Anyways, questions like these are better addressed to the beginners
list at yahoo groups.

Jonathan

On 1/24/07, Lucas Holland <hollandlucas@gmail.com> wrote:
> Hi, why does this function:
>
> let rec fact n =
>        n * fact (n-1);;
>
> yield an overflow error if called with n = 5?
>