caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Till Varoquaux" <till.varoquaux@gmail.com>
To: "Lucas Holland" <hollandlucas@gmail.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Simple factorial
Date: Tue, 23 Jan 2007 20:50:20 +0100	[thread overview]
Message-ID: <9d3ec8300701231150p76ecdedhd9d9d7f5be6b2d4e@mail.gmail.com> (raw)
In-Reply-To: <3E3A2218-5AA0-445F-A37D-B12EF4A2F9CB@gmail.com>

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
>


      parent reply	other threads:[~2007-01-23 19:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-01-23 18:50 Lucas Holland
2007-01-23 19:04 ` [Caml-list] " Tom
2007-01-23 19:10 ` robert
2007-01-23 19:11 ` Dário Abdulrehman
2007-01-23 19:50 ` Till Varoquaux [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9d3ec8300701231150p76ecdedhd9d9d7f5be6b2d4e@mail.gmail.com \
    --to=till.varoquaux@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=hollandlucas@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).