// Abstract ------------------------------------------------------------------------------------------------------------------------------

This sample solves the classic 'Tower of Hanoi' puzzle where the goal is to move N disks from the left peg to the right peg using the center peg as an auxiliary holding peg. It is based on John R. Fisher's Prolog tutorial.

// Examples ------------------------------------------------------------------------------------------------------------------------------

?- #move(3,left,right,center)
move top disk from left to right
move top disk from left to center
move top disk from right to center
move top disk from left to right
move top disk from center to left
move top disk from center to right
move top disk from left to right
-> ( ) := 1.00 (0.013) 1
 
// Code ----------------------------------------------------------------------------------------------------------------------------------

move {

    (1,:X,:Y,_)             :- console.puts("move top disk from ",:X," to ",:Y);
    (:N ?[gt(1)],:X,:Y,:Z)  :- sub(:N,1,:M), #move(:M,:X,:Z,:Y), #move(1,:X,:Y,_), #move(:M,:Z,:Y,:X);

}

// ---------------------------------------------------------------------------------------------------------------------------------------

[Home] [Email] [Twitter] [LinkedIn]