Jump to content

Towers of Hanoi: Difference between revisions

Line 210:
return moves
end hanoi</lang>
 
Alternatively, to dispense with a mutating global variable, we could write:
 
<lang applescript>
map(hanoi(3, "left", "right", "mid"), arrows)
 
-- n -> s -> s -> s -> [[s, s]]
on hanoi(n, a, b, c)
if n > 0 then
hanoi(n - 1, a, c, b) & {{a, b}} & hanoi(n - 1, c, b, a)
else
{}
end if
end hanoi
 
-- DISPLAY FUNCTION
on arrows(x)
item 1 of x & " -> " & item 2 of x
end arrows
 
-- LIBRARY FUNCTION
-- [a] -> (a -> b) -> [b]
on map(xs, f)
script mf
property lambda : f
end script
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to mf's lambda(item i of xs, i, xs)
end repeat
return lst
end map</lang>
 
{{Out}}
 
<pre>{"left -> right", "left -> mid", "right -> mid", "left -> right", "mid -> left", "mid -> right", "left -> right"}</pre>
 
=={{header|AutoHotkey}}==
<lang AutoHotkey>move(n, from, to, via) ;n = # of disks, from = start pole, to = end pole, via = remaining pole
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.