EricIO / cat-language

Automatically exported from code.google.com/p/cat-language
Other
0 stars 0 forks source link

Example of factorial is incorrect #18

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Enter the code for factorial online (see below for link)
   define rec_fac
      { dup 1 > [] [dec rec_fac *] if }
2. Attempt to compute the factorial of some number (specefically, 1 and
something greater than 1).

What is the expected output? What do you see instead?
Two problems occur.  In the base cases this recurses instead of doing
nothing, so the comparison is flipped (or the blocks are in the wrong
order).  Second, once that works the recursive case computes n - 1, then
the factorial of that, and then does multiply -- but there is nothing else
on the stack.  Hence, we need an additional dup.

Here is a working version of the above function as tested in my scheme
interpreter.
   (define-cat rec_fac => 
     dup 1 lteq_int [] [dup dec rec_fac mul_int] if)

Please provide any additional information below.
http://www.cat-language.com/manual.html

Original issue reported on code.google.com by bjchamb...@gmail.com on 3 Oct 2007 at 6:34