Version française
Home     About     Download     Resources     Contact us    
Browse thread
Factorial function
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jonathan Roewen <jonathan.roewen@g...>
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?
>
> Thanks in advance,
>
> Lucas
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>