Posted at

メタマジック・ゲームからハノイの塔写経

More than 5 years have passed since last update.


(define carry-one-disk
(lambda (sn dn)
(string-append sn dn)))

(define move-tower
(lambda (n sn dn hn)
(cond ((= n 1) (carry-one-disk sn dn))
(else (list (move-tower (sub1 n) sn hn dn)
(carry-one-disk sn dn)
(move-tower (sub1 n) hn dn sn))))))

(define carry-one-disk-flatten
(lambda (sn dn)
(list (string-append sn dn))))

(define move-tower-flatten
(lambda (n sn dn hn)
(cond ((= n 1) (carry-one-disk-flatten sn dn))
(else (append (move-tower-flatten (sub1 n) sn hn dn)
(carry-one-disk-flatten sn dn)
(move-tower-flatten (sub1 n) hn dn sn))))))