Towers of Hanoi: Difference between revisions

→‎{{header|AppleScript}}: minor changes to type signature, primitives. Hanoi function moved to top.
(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>on run
-- hanoi :: Int -> (String, String, String) -> [(String, String)]
map(arrows,on hanoi(3n, "left"{a, "right"b, "mid")c})
-- {"left -> right", "left -> mid", "right -> mid",
-- "left -> right", "mid -> left", "mid -> right", "left -> right"}
end run
 
 
-- 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
{}
Line 239 ⟶ 230:
 
 
-- TEST
-- DISPLAY FUNCTION
 
-- arrow :: (aString, aString) -> String
on arrows(x)
on arrow(tuple)
item 1 of xtuple & " -> " & item 2 of xtuple
end arrows
end arrow
 
on run
map(arrow, ¬
hanoi(3, {"left", "right", "mid"}))
--> {"left -> right", "left -> mid", "right -> mid", "left -> right",
-- "left -> right", "mid -> left", "mid -> right", "left -> right"}
end run
 
 
 
-- LIBRARY FUNCTIONFUNCTIONS
 
-- LIBRARY FUNCTION
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
scripttell mfmReturn(f)
propertyset lambdalng :to flength of xs
end script set lst to {}
repeat with i from 1 to lng
set lngend of lst to lengthlambda(item i of xs, i, xs)
set lst to {} end repeat
repeat with i from 1return to lnglst
end tell
set end of lst to mf's lambda(item i of xs, i, xs)
end repeatmap
 
return lst
-- 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
end mapmReturn</lang>
 
{{Out}}
9,659

edits