TowersOfHanoi.frink

Download or view TowersOfHanoi.frink in plain text format


/** This program implements a recursive Towers of Hanoi solver.

    See:
      https://en.wikipedia.org/wiki/Tower_of_Hanoi
*/


/** Set up the recursive call for n disks */
hanoi[n] := hanoi[n, 1, 3, 2]

/** The recursive call. */
hanoi[n, source, target, aux] :=
{
   if n > 0
   {
      // move n-1 disks from source to aux, so they are out of the way
      // This inverts the target and aux stacks.
      hanoi[n-1, source, aux, target]

      // move the nth disk from source to target
      println["Move from $source to $target"]
//      target.push[source.pop[]]

      // move the n-1 disks that we left on auxiliary onto target
      // This inverts the source and aux stacks.
      hanoi[n-1, aux, target, source]
   }
}

hanoi[7]


Download or view TowersOfHanoi.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 20217 days, 23 hours, 17 minutes ago.