0

I am trying to write a hyperoperation program in MIT/GNU-Scheme however am having some trouble, I have written individual ones up to n=5 working but would like to make one that functions does them all. I will include some of my failed attempts below.

(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)))))))
sklss
  • 3
  • 1

1 Answers1

2

According to the definition in wikipedia, there is an error in the last line of your last definition. It should be:

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

instead of:

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

So a possible correct recursive definition could be:

(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))))))
Renzo
  • 26,848
  • 5
  • 49
  • 61
  • Thanks, this was one of my errors. On top of that the answers I worked out on paper to check the code were incorrect. Thank you. – sklss Apr 07 '19 at 01:56