caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Jonathan Roewen" <jonathan.roewen@gmail.com>
To: "Lucas Holland" <hollandlucas@gmail.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Factorial function
Date: Wed, 24 Jan 2007 09:12:53 +1300	[thread overview]
Message-ID: <ad8cfe7e0701231212x6234a825h6fd07ae51002d281@mail.gmail.com> (raw)
In-Reply-To: <8C584854-A0BC-4926-A4E7-60620DF4D995@gmail.com>

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
>


      parent reply	other threads:[~2007-01-23 20:12 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-01-23 18:53 Lucas Holland
2007-01-23 18:59 ` [Caml-list] " Steve Taylor
2007-01-23 19:26 ` William D. Neumann
2007-01-23 20:12 ` Jonathan Roewen [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=ad8cfe7e0701231212x6234a825h6fd07ae51002d281@mail.gmail.com \
    --to=jonathan.roewen@gmail.com \
    --cc=caml-list@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).