尝试在方案中编写超操作

Trying to write hyper-operations in scheme

我正在尝试在 MIT/GNU-Scheme 中编写一个超操作程序,但是遇到了一些麻烦,我已经编写了最多 n=5 个的单个程序,但我想制作一个可以完成所有操作的程序。我将在下面包括一些我失败的尝试。

(define hyp (lambda (n x y)
  (hypiter n x x y)))
(define hypiter (lambda (lev an cou lim)
  (if (= lev 1) an
  (hypiter lev (hyp (- lev 1) an cou) cou lim))))
(define hyper (lambda (n x y)
  (hyper n x x y)))
(define hyperiter (lambda (lev an cou lim)
  (if (= lev 1) (+ an cou)
  (hyper (- lev 1) an cou))))
(define h (lambda (n a b)
  (cond
    ((= n 1) (+ a b))
    ((= b 1) (- n 1))
    (else (h (- n 1) (h (- n 1) a a) (- b 1)))))))
(define hyperoperation (lambda (n a b)
  (cond
    ((= n 0) (+ 1 b))
    ((and (= n 1) (= b 0)) a)
    ((and (= n 2) (= b 0)) 0)
    ((and (>= n 3) (= b 0)) 1)
    (else (hyperoperation (- b 1) a (hyperoperation n a (- b 1)))))))

根据wikipedia中的定义,你上次定义的最后一行有错误。应该是:

(else (hyperoperation (- n 1) a (hyperoperation n a (- b 1))))))

而不是:

(else (hyperoperation (- b 1) a (hyperoperation n a (- b 1)))))))

所以一个可能的正确递归定义可能是:

(define (hyperoperation n a b)
  (cond ((= n 0) (+ b 1))
        ((= b 0) (cond ((= n 1) a)
                       ((= n 2) 0)
                       (else 1)))
        (else (hyperoperation (- n 1) a (hyperoperation n a (- b 1))))))