【发布时间】:2011-07-25 14:23:56
【问题描述】:
在这里问它很痛苦。确实如此。每次我徒劳地寻找问题的答案时,我都会看到它。嘲讽我。 Stack Overflow.
不管怎样,一些地狱般的影响使我试图解决河内塔。我的第一个解决方案不完整,因为如果使用太多磁盘运行,它会导致 memory error:
(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))))))
我在某处读到延续传递风格可以解决问题。不过这个didn't help either:
(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))))))))))))
【问题讨论】:
-
多少磁盘是磁盘太多,出于好奇?
-
@dyoo for
hanoi:19 个磁盘;对于hanoi_cps:15 个磁盘
标签: recursion scheme tail-recursion towers-of-hanoi