Towers of Hanoi: Difference between revisions

Content added Content deleted
m (→‎{{header|AppleScript}}: (normalised argument order of map to map :: (a -> b) -> [a] -> [b] ))
Line 1,714: Line 1,714:
}
}
move(4, "A", "B", "C");</lang>
move(4, "A", "B", "C");</lang>



Or, as a functional expression, rather than a statement with side effects:
Or, as a functional expression, rather than a statement with side effects:


<lang JavaScript>(function () {
<lang JavaScript>(function () {

// n -> s -> s -> s -> [[s, s]]
// hanoi :: n -> s -> s -> s -> [[s, s]]
function hanoi(n, a, b, c) {
function hanoi(n, a, b, c) {
return n ? hanoi(n - 1, a, c, b).concat(
return n ? hanoi(n - 1, a, c, b).concat(
Line 1,725: Line 1,726:
).concat(hanoi(n - 1, c, b, a)) : [];
).concat(hanoi(n - 1, c, b, a)) : [];
}
}

return hanoi(3, 'left', 'right', 'mid').map(function (d) {
return hanoi(3, 'left', 'right', 'mid')
.map(function (d) {
return d[0] + ' -> ' + d[1];
return d[0] + ' -> ' + d[1];
});
});
})();</lang>
})();</lang>