cwtests/cw05/hanoi.fun
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 25 Sep 2023 15:12:11 +0100
changeset 929 9541e073f2ed
parent 856 23273e3a120f
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
856
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Towers of Hanoi in Fun
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
def hanoi(n: Int, a: Int, b: Int, c: Int) : Void =
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
  if n != 0 then {
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
    hanoi(n - 1, a, c, b);
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
    print_int(a);
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
    print_char('-'); print_char('>'); // prints out "->"
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
    print_int(b);
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
    print_char('\n');
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
    hanoi(n - 1, c, b, a)
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
  } else skip;
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
23273e3a120f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
hanoi(4,1,2,3)