Towers of Hanoi: Difference between revisions
Content added Content deleted
m (oct...) |
(add Tcl) |
||
Line 638: | Line 638: | ||
end if; |
end if; |
||
end func; |
end func; |
||
=={{header|Tcl}}== |
|||
The use of <code>interp alias</code> shown is a sort of closure: keep track of the number of moves required |
|||
<lang tcl>interp alias {} hanoi {} do_hanoi 0 |
|||
proc do_hanoi {count n {from A} {to C} {via B}} { |
|||
if {$n == 1} { |
|||
interp alias {} hanoi {} do_hanoi [incr count] |
|||
puts "$count: move from $from to $to" |
|||
} else { |
|||
incr n -1 |
|||
hanoi $n $from $via $to |
|||
hanoi 1 $from $to $via |
|||
hanoi $n $via $to $from |
|||
} |
|||
} |
|||
hanoi 4</lang> |
|||
produces |
|||
<pre>1: move from A to B |
|||
2: move from A to C |
|||
3: move from B to C |
|||
4: move from A to B |
|||
5: move from C to A |
|||
6: move from C to B |
|||
7: move from A to B |
|||
8: move from A to C |
|||
9: move from B to C |
|||
10: move from B to A |
|||
11: move from C to A |
|||
12: move from B to C |
|||
13: move from A to B |
|||
14: move from A to C |
|||
15: move from B to C</pre> |
|||
=={{header|Toka}}== |
=={{header|Toka}}== |