computing factorial (1)
specification
- want (factorial k) –> k!
- k! = 1 * 2 * 3 * .. * k
a recursive definition of factorial
- if k = 1, k! = 1
- otherwise k! = (k-1)! * k
a recursive procedure
- (define (factorial k) (if (= k 1) 1 (* k (factorial (- k 1)))))
puzzle
- how can factorial solve a problem by calling itself?
- each time, it calls itself on a smaller problem