1

In PLAI's chapter9 "Understanding Recursion", at the beginning, there is an example factorial:


(with (fac (fun (n)
                (if0 n 
                     1
                     (* n (fac (+ n -1))))))

At page 90, the author said "Before you continue reading, please pause for a moment, study the program carefully, write down the environments at each stage, step by hand through the interpreter, even run the program if you wish, to convince yourself that this error will occur. Understanding the error thoroughly will be essential to following the remainder of theis section."
But I am not sure that I can write down the steps completely, someone would like to help me write down the steps ? Thanks a lot in advance!

abelard2008
  • 1,984
  • 1
  • 20
  • 35
  • sorry i'm not familiar with racket syntax (with ... etc), but from a first look maybe it could be that if this function gets a negative argument it's going down to -MAXINT or whatever the platform supports. – ramrunner Aug 17 '12 at 18:05
  • No, not that. The problem has nothing to do with underflow. It's an **environment** problem. – dyoo Aug 18 '12 at 13:51

2 Answers2

2

Let your interpreter print out the steps for you.

(define (interp expr env)
  (displayln (list 'expr expr 'env env))
  (type-case CFAE/L expr
soegaard
  • 30,661
  • 4
  • 57
  • 106
0

after reading and running the chapter 10's code, and independently executed the middle steps of the above program,such as


(interp  (if0 (id 'n) (num 1) (mult (id 'n) (app (id 'fac) (sub (id 'n) (num 1)))))
           (aSub 'n
                 (numV 2)
                 (closureV-env (lookup 'fac new-env))))

I can write down the steps by hand! Thank every one!

abelard2008
  • 1,984
  • 1
  • 20
  • 35