今天在scheme中写了如下代码,但是求值错误。请不要告诉我我编程很糟糕,我知道这是一个经典的递归问题,但我遇到了麻烦:
(define (towers-of-hanoi n source temp dest)
(if (= n 1)
(begin (display "Move the disk from ")
(display source)
(display " to " )
(display dest)
(newline))
(begin (towers-of-hanoi (- n 1) source temp dest)
(display "Move the disk from ")
(display source)
(display " to ")
(display dest)
(newline)
(towers-of-hanoi(- n 1) temp source dest))))
Run Code Online (Sandbox Code Playgroud)
我期望代码能够工作,当我调试它时,我只是更加困惑。谁能帮我?
以下函数是尾递归的吗?如果没有,我可以做些什么来修改它?
(define (euclids-alg n1 n2)
(cond((= n1 0) n2)
((= n2 0) n1)
((= n1 n2) n1)
((> n1 n2) (euclids-alg (- n1 n2) n2))
((< n1 n2) (euclids-alg n1 (- n2 n1)))))
Run Code Online (Sandbox Code Playgroud)