My favorites | Sign in
Project Logo
                
New issue | Search
for
| Advanced search | Search tips
Issue 18: Example of factorial is incorrect
1 person starred this issue and may be notified of changes. Back to list
Status:  New
Owner:  ----
Type-Defect
Priority-Medium


Sign in to add a comment
 
Reported by bjchambers, Oct 03, 2007
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
Sign in to add a comment

Hosted by Google Code