Guest User

Untitled

a guest
Mar 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. type rod = list(int);
  2.  
  3. exception InvalidMove(int, int);
  4.  
  5. exception OtherMove(rod, rod);
  6.  
  7. let moveRods = (a: rod, b: rod) : (rod, rod) =>
  8. switch (a, b) {
  9. | ([x, ...xs], []) => (xs, [x])
  10. | ([x, ...xs], [y, ...ys]) when x < y => (xs, [x, y, ...ys])
  11. | ([x, ...xs], [y, ...ys]) => raise(InvalidMove(x, y))
  12. | (xs, ys) => raise(OtherMove(xs, ys))
  13. };
  14.  
  15. let rec move = (n: int, src: rod, dst: rod, aux: rod) : (rod, rod, rod) =>
  16. if (n > 0) {
  17. let (src, aux, dst) = move(n - 1, src, aux, dst);
  18. let (src, dst) = moveRods(src, dst);
  19. let (aux, dst, src) = move(n - 1, aux, dst, src);
  20. (src, dst, aux);
  21. } else {
  22. (src, dst, aux);
  23. };
  24.  
  25. let hanoi = (src: rod) => move(src |> List.length, src, [], []);
  26.  
  27. let _init = Array.init(23, x => x) |> Array.to_list;
  28.  
  29. let (a, b, c) = hanoi(_init);
  30.  
  31. Js.log(b); /*would be exactly same as `a`*/
Add Comment
Please, Sign In to add comment