Object not applicable in MIT Scheme (a different Ackermann's function)

302 views Asked by At

I found this version of Ackermann's function and tried to code it in MIT Scheme Lisp with no success:

The Ackermann Function A(m,n)

When m=0

A(m,n)=n+1

When m>0 and n=0

A(m,n)=A(m-1,1)

When m>0 and n>0

A(m,n)=A(m-1,A(m,n-1))

(found here http://www.gfredericks.com/sandbox/arith/ackermann)

My Scheme code:

(define (acker2 m n)
        (cond ((= m 0)
               (+ n 1))
              ((and (> m 0)
                    (= n 0))
               (acker2 (- m 1)
                       1))
              ((and (> m 0)
                    (> n 0))
               (acker2 (- m 1)
                       (acker2 (m
                               (- n 1)))))))

Now some results:

(acker2 0 0) value: 1

(acker2 0 1) value: 2

(acker2 0 2) value: 3

(acker2 2 2) object 2 is not applicable

(acker2 1 23) object 1 is not applicable

(acker2 8 0) object 7 is not applicable

What's the solution?

1

There are 1 answers

0
Renzo On BEST ANSWER

There is an error (too many parentheses) in the last expression:

(acker2 (m (- n 1)))

this should be:

(acker2 m (- n 1))

Remember that in Lisp/Scheme (a b1 b2 ...) means “apply the function a to the arguments b1 b2 ...”. The message “object 2 is not applicable” means exactly this: m is equal to 2 and the system tries to apply it to (- n 1). But the number 2 (“the object 2”) is not a function (“is not applicable”).