Towers of Hanoi: Difference between revisions

+Icon+Unicon
No edit summary
(+Icon+Unicon)
Line 433:
putStrLn $ "Move " ++ show a ++ " to " ++ show b
hanoiM' (n-1) c b a</lang>
 
== Icon and Unicon ==
The following is based on a solution in the Unicon book.
==={{header|Icon}}===
<lang Icon>procedure main(arglist)
hanoi(arglist[1]) | stop("Usage: hanoi n\n\rWhere n is the number of disks to move.")
end
 
#procedure hanoi(n:integer, needle1:1, needle2:2) # unicon shorthand for icon code 1,2,3 below
 
procedure hanoi(n, needle1, needle2) #: solve towers of hanoi by moving n disks from needle 1 to needle2 via other
local other
/needle1 := 1 # 1 default
/needle2 := 2 # 2 default
n := integer(0 < n) | runerr(n,101) # 3 ensure integer (this also ensures it's positive too)
 
if n = 1 then
write("Move disk from ", needle1, " to ", needle2)
else {
other := 6 - needle1 - needle2 # clever but somewhat un-iconish way to find other
hanoi(n-1, needle1, other) # n-1, src, via, dest
write("Move disk from ", needle1, " to ", needle2)
hanoi(n-1, other, needle2) # n-1, via, dist, src
}
return
end</lang>
==={{header|Unicon}}===
This Icon solution works in Unicon.
 
=={{header|Io}}==
Anonymous user