1 2(********************************************************************) 3(* *) 4(* hanoi.sd7 Solve the tower of hanoi problem *) 5(* Copyright (C) 1991, 1992, 1993, 1994, 2004 Thomas Mertes *) 6(* *) 7(* This program is free software; you can redistribute it and/or *) 8(* modify it under the terms of the GNU General Public License as *) 9(* published by the Free Software Foundation; either version 2 of *) 10(* the License, or (at your option) any later version. *) 11(* *) 12(* This program is distributed in the hope that it will be useful, *) 13(* but WITHOUT ANY WARRANTY; without even the implied warranty of *) 14(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *) 15(* GNU General Public License for more details. *) 16(* *) 17(* You should have received a copy of the GNU General Public *) 18(* License along with this program; if not, write to the *) 19(* Free Software Foundation, Inc., 51 Franklin Street, *) 20(* Fifth Floor, Boston, MA 02110-1301, USA. *) 21(* *) 22(********************************************************************) 23 24 25$ include "seed7_05.s7i"; 26 include "stdio.s7i"; 27 28 29const proc: hanoi (in integer: disk, in string: source, in string: dest, in string: via) is func 30 begin 31 if disk > 0 then 32 hanoi(pred(disk), source, via, dest); 33 writeln("Move disk " <& disk <& " from " <& source <& " to " <& dest); 34 hanoi(pred(disk), via, dest, source); 35 end if; 36 end func; 37 38 39const proc: hanoi (in integer: height) is func 40 begin 41 writeln("To move a tower of height " <& height <& " from left to right:"); 42 writeln; 43 hanoi(height, "left", "right", "middle"); 44 writeln; 45 end func; 46 47 48const proc: main is func 49 begin 50 writeln; 51 writeln("Tower of Hanoi:"); 52 writeln; 53 hanoi(4); 54 end func; 55