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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Till Varoquaux <till.varoquaux@g...>
Subject: Re: [Caml-list] Simple factorial
Well that's quite a basic error you've got here:
You are making an induction without a base case

a correct implementation of factorial could be:

 let rec factorial= function
  | 0 -> 1
  | n -> n*factorial(n-1);;

This implementation is suboptimal (not tail recursive etc...) but
should be good enough for a beginner.

And since we brought this up, you might want to ask those questions on
the OCaml Beginners list (ocaml_beginners@yahoogroups.com). It seems a
more appropriate place. I also believe some googling could have helped
you, for instance
(http://www.ffconsultancy.com/products/ocaml_for_scientists/chapter1.html)
covers this topic more in depth.

Cheers,
Till

On 1/23/07, Lucas Holland <hollandlucas@gmail.com> wrote:
> Hello,
>
> I've just started learning O'Caml. I've written a simple factorial
> function (no checking whether n is 1 etc.):
>
> let rec factorial n =
>         n * factorial (n-1);;
>
> When I call this function with let's say 5 as an argument, I get an
> overflow error message.
>
> Any ideas?
>
> chell
>
> _______________________________________________
> 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
>