home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / elk-2_0.lha / elk-2.0 / examples / scheme / hanoi < prev    next >
Encoding:
Text File  |  1989-02-17  |  399 b   |  21 lines

  1. ;;; -*-Scheme-*-
  2. ;;;
  3. ;;; Towers of Hanoi
  4.  
  5. (define (hanoi n)
  6.   (if (zero? n)
  7.       (display "Huh?\n")
  8.       (transfer 'A 'B 'C n)))
  9.  
  10. (define (print-move from to)
  11.   (format #t "Move disk from ~s to ~s~%" from to))
  12.  
  13. (define (transfer from to via n)
  14.   (if (= n 1)
  15.       (print-move from to)
  16.       (transfer from via to (1- n))
  17.       (print-move from to)
  18.       (transfer via to from (1- n))))
  19.  
  20. (hanoi 3)
  21.