Version française
Home     About     Download     Resources     Contact us    

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

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: 2007-01-23 (19:27)
From: William D. Neumann <wneumann@c...>
Subject: Re: [Caml-list] Factorial function
On Tue, 23 Jan 2007, Lucas Holland wrote:

> Hi, why does this function:
> let rec fact n =
> 	n * fact (n-1);;
> yield an overflow error if called with n = 5?

It overflows because you don't provide a base case for the recursion (e.g. 
if n = 1 then 1 else ...), it simply cycles backwards through the ints 
until the stack is exhausted.

Also, if you're just learning OCaml, you may want to use the Caml 
Beginners list for these types of questions.  You con find the list 
information at:

William D. Neumann


"There's just so many extra children, we could just feed the
children to these tigers.  We don't need them, we're not doing 
anything with them.

Tigers are noble and sleek; children are loud and messy."

         -- Neko Case

Life is unfair.  Kill yourself or get over it.
 	-- Black Box Recorder