* Simple factorial
@ 2007-01-23 18:50 Lucas Holland
2007-01-23 19:04 ` [Caml-list] " Tom
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Lucas Holland @ 2007-01-23 18:50 UTC (permalink / raw)
To: caml-list
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Simple factorial
2007-01-23 18:50 Simple factorial Lucas Holland
@ 2007-01-23 19:04 ` Tom
2007-01-23 19:10 ` robert
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Tom @ 2007-01-23 19:04 UTC (permalink / raw)
To: Lucas Holland; +Cc: caml-list
[-- Attachment #1: Type: text/plain, Size: 1332 bytes --]
On 23/01/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?
>
Well... the problem is, that your function "never ends" - the multiplication
continues into infinity. When you make a call
factorial 5
what actually happens is
5 * factorial (5-1)
5 * factorial 4
5 * 4 * factorial 3
5 * 4 * 3 * factorial 2
5 * 4 * 3 * 2 * factorial 1
5 * 4 * 3 * 2 * 1 * factorial 0
5 * 4 * 3 * 2 * 1 * 0 * factorial (-1)
5 * 4 * 3 * 2 * 1 * 0 * (-1) * factorial (-2)
5 * 4 * 3 * 2 * 1 * 0 * (-1) * (-2) * factorial (-3)
.
.
.
and your function never returns. Checking if n is 1 (or 0) isn't just
neccessary because of the matematical definition of the factorial, but also
because of algorithmic implementation.
The overflow exception you get is generated when a lot of functions are
called (usually this happens in non-tail-recursive list functions with very
big input lists) but don't return, and so they consume all the stack
available.
- Tom
[-- Attachment #2: Type: text/html, Size: 2232 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Simple factorial
2007-01-23 18:50 Simple factorial 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
3 siblings, 0 replies; 5+ messages in thread
From: robert @ 2007-01-23 19:10 UTC (permalink / raw)
To: caml-list
> 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?
>
You haven't defined a stop condition, so it's going down and down into the
negatives until it overflows.
~~ Robert.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Simple factorial
2007-01-23 18:50 Simple factorial 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
3 siblings, 0 replies; 5+ messages in thread
From: Dário Abdulrehman @ 2007-01-23 19:11 UTC (permalink / raw)
To: Lucas Holland; +Cc: caml-list
[-- Attachment #1: Type: text/plain, Size: 933 bytes --]
Hi,
Join http://tech.groups.yahoo.com/group/ocaml_beginners/ .
The yahoo list is more appropriate for beginner questions (I am a beginner
too).
Anyway, whatever the programming language you are using, you need to define
a base case for recursion...
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
>
[-- Attachment #2: Type: text/html, Size: 1602 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Simple factorial
2007-01-23 18:50 Simple factorial Lucas Holland
` (2 preceding siblings ...)
2007-01-23 19:11 ` Dário Abdulrehman
@ 2007-01-23 19:50 ` Till Varoquaux
3 siblings, 0 replies; 5+ messages in thread
From: Till Varoquaux @ 2007-01-23 19:50 UTC (permalink / raw)
To: Lucas Holland; +Cc: caml-list
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
>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2007-01-24 17:14 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-01-23 18:50 Simple factorial 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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox