Towers of Hanoi: Difference between revisions
Content added Content deleted
(Lingo added) |
(→{{header|AppleScript}}: minor changes to type signature, primitives. Hanoi function moved to top.) |
||
Line 219:
Or dispensing with a mutating global variable, we could write:
<lang applescript>
-- hanoi :: Int -> (String, String, String) -> [(String, String)]
-- {"left -> right", "left -> mid", "right -> mid", ▼
-- "left -> right", "mid -> left", "mid -> right", "left -> right"} ▼
end run▼
if n > 0 then
hanoi(n - 1, {a, c, b}) & {{a, b}} & hanoi(n - 1, {c, b, a})
else
{}
Line 239 ⟶ 230:
-- TEST
-- arrow :: (
on arrow(tuple)
item 1 of
end arrow
on run
map(arrow, ¬
hanoi(3, {"left", "right", "mid"}))
▲ --> {"left -> right", "left -> mid", "right -> mid", "left -> right",
▲end run
▲-- LIBRARY FUNCTION
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
repeat with i from 1 to lng
set
end tell
-- Lift 2nd class handler function into 1st class script wrapper
end map</lang>▼
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
▲ f
▲ else
script
property lambda : f
end script
end if
{{Out}}
|