Towers of Hanoi: Difference between revisions
→{{header|UNIX Shell}}: make portable to other shells
(→{{header|6502 Assembly}}: Add implementation.) |
(→{{header|UNIX Shell}}: make portable to other shells) |
||
Line 6,615:
=={{header|UNIX Shell}}==
{{works with|
{{works with|Korn Shell}}
<lang bash>#!/bin/bash▼
{{works with|Z Shell}}
<lang bash>function move {
typeset -i n=$1
if (( n )); then
▲ local from="$2"
▲ local to="$3"
▲ local via="$4"
if [[ "$n" == "1" ]]▼
echo "Move disk from pole $from to pole $to"
▲ move $(($n - 1)) $from $via $to
▲ move $(($n - 1)) $via $to $from
fi
}
move "$
A strict POSIX (or just really old) shell has no subprogram capability, but scripts are naturally reentrant, so:
{{works with|Bourne Shell}}
{{works with|Almquist Shell}}
"$0" "`expr $1 - 1`" "$2" "$4" "$3"
echo "Move disk from pole $2 to pole $3"
"$0" "`expr $1 - 1`" "$4" "$3" "$2"
fi
</lang>
Output from any of the above:
{{Out}}
<pre>$ hanoi 4 1 3 2
Move disk from pole 1 to pole 2
Move disk from pole 1 to pole 3
Move disk from pole 2 to pole 3
Move disk from pole 1 to pole 2
Move disk from pole 3 to pole 1
Move disk from pole 3 to pole 2
Move disk from pole 1 to pole 2
Move disk from pole 1 to pole 3
Move disk from pole 2 to pole 3
Move disk from pole 2 to pole 1
Move disk from pole 3 to pole 1
Move disk from pole 2 to pole 3
Move disk from pole 1 to pole 2
Move disk from pole 1 to pole 3
Move disk from pole 2 to pole 3</pre>
=={{header|Ursala}}==
|