Exercise 1.10
Tuesday, December 29th, 2009Given the following implementation of the Ackermann’s Function:
1 2 3 4 5 6 | (define (A x y) (cond ((= y 0) 0) ((= x 0) (* 2 y)) ((= y 1) 2) (else (A (- x 1) (A x (- y 1)))))) |
What are the evaluations of these expressions:
1 | (A 1 10) |
Value: 1024
1 | (A 2 4) |
Value: 65536
1 | (A 3 3) |
Value: 65536
What are the mathematical counterparts of these procedures:
1 | (define (f n) (A 0 n)) |
corresponds to:
1 2 3 4 5 6 7 8 | (define (g n) (A 1 n)) (A 0 (A 1 (- n 1)) (A 0 (A 0 (A 1 (- n 2)))) (A 0 (A 0 (A 0 (A 1 (- n 3))))) . . . |
corresponds to:
1 2 3 4 5 6 7 8 9 | (define (h n) (A 2 n)) (A 2 n) (A 1 (A 2 (- n 1))) (A 1 (A 1 (A 2 (- n 2)))) (A 1 (A 1 (A 1 (A 2 (- n 3))))) . . . |
At some step i, the most inner combination in the form (A 2 (- n i)) will evaluate to 2 cause n-i=1. (A 1 x) is 2^x by the definition of g(n), and there will be n evaluations of such combination, resulting in:
This can be also expressed as a recurrence: