Monadically Speaking: Benjamin’s Adventures in PLT Wonderland

November 18, 2008

Towers of Hanoi

Filed under: Scheme — Benjamin L. Russell @ 12:42 pm

Ladies and Gentlemen!  Welcome to DekuDekuplex’s Blog, the Never Never Land of amateur functional programming theory!

What is the value of a process that doesn’t terminate?

Allow me to present my introductory Scheme procedure, Towers of Hanoi:

;; Towers of Hanoi, plt1.1
;; PLT-Scheme-specific Version 1.1 of Towers of Hanoi
;; 
;; Copyright(C) April 02, 2008, at 19:38, 
;; by Benjamin L. Russell

(define (hanoi n)
  (hanoi-helper 'A 'B 'C n))

(define (hanoi-helper source using destination n)
  (cond ((= n 1)
         (printf "Moving from disc ~a to ~a.\n" source destination))
        (else
         (hanoi-helper source destination using (- n 1))
         (hanoi-helper source using destination 1)
         (hanoi-helper using source destination (- n 1)))))

To run the Scheme source code above, simply type:

(hanoi 3)

You shall be rewarded with:

Moving from disc A to C.
Moving from disc A to B.
Moving from disc C to B.
Moving from disc A to C.
Moving from disc B to A.
Moving from disc B to C.
Moving from disc A to C.

Wonderful, isn’t it?

So much for now.  Until next time….

(lambda x.(x x))(lambda x.(x x))

3 Comments »

  1. [...] a solution, this function had never seemed an entirely satisfactory counterpart to my earlier Scheme solution (the output thereof has been slightly reworded from “… from disc” to [...]

    Pingback by Climbing the Monadic Tower: Rewriting the Towers of Hanoi Solution « Monadically Speaking: Benjamin’s Adventures in PLT Wonderland — April 13, 2009 @ 7:59 pm | Reply

  2. [...] a solution, this function had never seemed an entirely satisfactory counterpart to my earlier Scheme solution (the output thereof has been slightly reworded from “… from disc” to [...]

    Pingback by Climbing the Towers of Hanoi with Haskell-style Curry from a Monadic Container (While Sparing the Sugar!) « Monadically Speaking: Benjamin’s Adventures in PLT Wonderland — April 14, 2009 @ 2:22 pm | Reply

  3. [...] a solution, this function had never seemed an entirely satisfactory counterpart to my earlier Scheme solution (part of the text in the output statement thereof below has been slightly reworded from [...]

    Pingback by Climbing the Towers of Hanoi with Haskell-style Curry from a Monadic Container (While Sparing the Sugar!) « Monadically Speaking: Benjamin’s Adventures in PLT Wonderland — April 14, 2009 @ 3:37 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.