Hi. I am new to scheme and I am trying to get this to work correctly.
This is a recursive function that crashes on my linux box with C++. I was
told to get scheme cause it was made for recursion.
This is Ackerman's function. You give it 2 integers: m and n
If m = 0 return 2*n
If m >= 1 and n=0 return 0
If m >= 1 and n=1 return 2
If m >= 1 and n>=2 then recursivly call 'ackerman(m-1, ackerman(m,n-1))'
Here is my translated scheme. Again, I am not too familiar with it. I made
this in about 20 min. If you have a better more efficient way, please pass
is along.
(define ackerman
(lambda (m n)
(cond ((= m 0)
(* 2 n))
((and (>= m 1) (= n 0))
0)
((and (>= m 1) (= n 1))
2)
(else
(ackerman (- m 1) (ackerman m (- n 1)))))
))
Here are some tests on the above code. All of these are correct.
; > (ackerman 1 0)
; 0
; > (ackerman 2 2)
; 4
; > (ackerman 2 3)
; 16
; > (ackerman 3 3)
; 65536
I cannot get this one to work: (ackerman 3 4)
I left my comp running for about 1 min before I stopped it and it still
hadn't given me anything.
I would appreciate some help on this.
Thanks,
Drmac
|