Solving puzzles is a challenge developers tend to enjoy and provides points of comparison between different programming languages. Two puzzles are presented here to show TAQ dealing with challenges well known to the computing community: "Towers of Hanoi" and Sudoku.
Towers of Hanoi
Towers of Hanoi is often used as an example of recursion where a function calls itself one or more times to progress to a result. The puzzle consists of three rods and a number of disks of different sizes, which can slide onto any rod. The initial configuration is a stack of disks on one rod and the aim is to move the stack to the adjacent rod while keeping to the rules governing the placement of the disks:
- For each move, take the disk from the top of one of the stacks and place it on top of another stack.
- No disk may be placed on top of a smaller disk.
The puzzle is solved for a 1, 2 and 3 disks in towers-of-hanoi.taq. As the solution depends entirely on recursion, the program contains a single flow called "tower_of_hanoi".
(
?? (n == 1)
tower_of_hanoi(disk = n-1, from=from_rod, to=aux_rod, aux=to_rod),
print("Move disk ", n, " from rod ", from_rod, " to rod ", to_rod),
tower_of_hanoi(disk = n-1, from=aux_rod, to=to_rod, aux=from_rod)
(from_rod="A", to_rod="C", aux_rod="B")
Two aspects of interest with recursion are how to set initial parameters to start it off and and how to exit from the recursion. The first aspect is taken care of by using parameters. Three template parameters set the names of the rods:
The disk number is supplied as a query parameter so that there can be queries for a specific number of disks:
query towers_of_hanoi2(tower_of_hanoi)(n=2);
query towers_of_hanoi3(tower_of_hanoi)(n=3)
The initial parameters are paired to flow variables by name, but this is not possible for the recursion parameters, so
they are paired by position. For example, the first parameter, the disk number "n", is set in the recursion
call to disk = n-1
, which works because it is the first parameter.
The second aspect of interest is the exit from the recursion and this is handled by a converse criterion which fires when the disk number gets to 1: