在这里问它很痛苦。确实如此。每次我徒劳地寻找问题的答案时,我都会看到它。嘲讽我。堆栈溢出。
无论如何,一些地狱般的影响使我试图解决河内塔。我的第一个解决方案不完整,因为如果使用太多磁盘运行会导致内存错误:
(define hanoi
(lambda (n from to other)
(cond ((< n 0)
(error `(Error! Number of disks ,n cannot be less than 0!)))
((= n 0)
'())
(else
(append (hanoi (- n 1)
from
other
to)
`((,from ,to))
(hanoi (- n 1)
other
to
from))))))
我在某处读到延续传递风格可以解决问题。但是,这也无济于事:
(define hanoi_cps
(lambda (n from to other c)
(cond ((< n 0)
(error `(Error! Number of disks ,n cannot be less than 0!)))
((= n 0)
(c '()))
(else
(hanoi_cps (- n 1)
from
other
to
(lambda (x)
((lambda (w)
(w `((,from ,to))))
(lambda (y)
(hanoi_cps (- n 1)
other
to
from
(lambda (z)
(c (append x y z))))))))))))