Towers of Hanoi - Programming Environment Lab - LISP

Program:
(defun hanoi(n)
    (dohanoi n 3 1 2)
)

(defun dohanoi(ndisks destination source temp)
    (cond
        ((> ndisks 0) (dohanoi (- ndisks 1) temp source destination)
                (format t "Move the top disk from peg~d to peg~d ~&" source destination)
                (dohanoi (- ndisks 1) destination temp source)
        )
    )
)

Output:
Break 4 [5]> (load 'h.lsp)
;; Loading file h.lsp ...
;; Loaded file h.lsp
T
Break 4 [5]> (hanoi 3)
Move the top disk from peg1 to peg3
Move the top disk from peg1 to peg2
Move the top disk from peg3 to peg2
Move the top disk from peg1 to peg3
Move the top disk from peg2 to peg1
Move the top disk from peg2 to peg3
Move the top disk from peg1 to peg3
NIL
Break 4 [5]>

1 comment:

Related Posts Plugin for WordPress, Blogger...