HOME | EDIT | RSS | ABOUT | GITHUB

理解并编写ackermann函数的scheme公式

这两天在学习《Structure and Interpretation of Computer Programs》(本书以简称SICP著名),虽然只看了个开头,但依然感受到了语言和数学的魅力。 细嚼慢咽的翻着书,力争学透书中列举的例子和练习,对于一些经典的函数表达式,试图去wiki寻找更详细的解释。 昨天看到练习10,讲了构造Ackermann函数,sicp中直接列出构造的函数:

(define (A x y)
  (cond ((= y 0) 0)
        ((= x 0) (* 2 y))
        ((= y 1) 2)
        (else (A (- x 1)
                 (A x (- y 1))))))

查阅wiki时,看到原自然函数公式是

(ignore)
A(m,n)={\begin{cases}n+1&{\mbox{if }}m=0\\A(m-1,1)&{\mbox{if }}m>0{\mbox{ and }}n=0\\A(m-1,A(m,n-1))&{\mbox{if }}m>0{\mbox{ and }}n>0.\end{cases}} 

这部分有乱码,直接搜索后看原文吧。 wiki ackermann function 在计算时,发现sicp中出现的答案和wiki中下面列表的答案不一致: Values of A(m, n)

m\n 0 1 2 3 4 n
0 1 2 3 4 5 n+1
1 2 3 4 5 6 n+2=2+(n+3)-3
2 3 5 7 9 11 2n+3=2⋅(n+3)-3
3 5 13 29 61 125 2^{(n+3)}-3
4 13 65533 265536−3 {2^{2^ {2^{2^{2^ {\displaystyle{\begin
| m\n |  0 |     1 |        2 |      3 |         4 | n                     |
|   0 |  1 |     2 |        3 |      4 |         5 | n+1                   |
|   1 |  2 |     3 |        4 |      5 |         6 | n+2=2+(n+3)-3         |
|   2 |  3 |     5 |        7 |      9 |        11 | 2n+3=2\cdot(n+3)-3    |
|   3 |  5 |    13 |       29 |     61 |       125 | 2^{(n+3)}-3           |
|   4 | 13 | 65533 | 265536−3 | {2^{2^ | {2^{2^{2^ | {\displaystyle{\begin |

经过仔细对比,发现公式表述有差异。在昨天晚上弄明原题后,今天又重新构建了wiki页面公式的scheme函数,一难,结果和wiki这列表中的答案一致。

(define (A x y)
  (cond ((= x 0)(+ y 1))
	((= y 0)(A (- x 1) 1))
	(else (A (- x 1)(A x (- y 1))))))