It hurts to ask it here. It really does. Every time I search in vain for the answers to my troubles, I see it. Taunting me. Stack Overflow.
Anyway, some hellish influence caused me to attempt to solve the Towers of Hanoi. My first solution was incomplete, as it resulted in a memory error if run with too many disks:
(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))))))
I read somewhere that continuation-passing style would solve the problem. However, this 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))))))))))))
hanoi
: 19 disks; forhanoi_cps
: 15 disks – Aerograph