Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type rod = list(int);
- exception InvalidMove(int, int);
- exception OtherMove(rod, rod);
- let moveRods = (a: rod, b: rod) : (rod, rod) =>
- switch (a, b) {
- | ([x, ...xs], []) => (xs, [x])
- | ([x, ...xs], [y, ...ys]) when x < y => (xs, [x, y, ...ys])
- | ([x, ...xs], [y, ...ys]) => raise(InvalidMove(x, y))
- | (xs, ys) => raise(OtherMove(xs, ys))
- };
- let rec move = (n: int, src: rod, dst: rod, aux: rod) : (rod, rod, rod) =>
- if (n > 0) {
- let (src, aux, dst) = move(n - 1, src, aux, dst);
- let (src, dst) = moveRods(src, dst);
- let (aux, dst, src) = move(n - 1, aux, dst, src);
- (src, dst, aux);
- } else {
- (src, dst, aux);
- };
- let hanoi = (src: rod) => move(src |> List.length, src, [], []);
- let _init = Array.init(23, x => x) |> Array.to_list;
- let (a, b, c) = hanoi(_init);
- Js.log(b); /*would be exactly same as `a`*/
Add Comment
Please, Sign In to add comment