solutions/cw5/hanoi.fun
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 04 Nov 2022 12:07:40 +0000
changeset 894 02ef5c3abc51
parent 867 solution/cw5/hanoi.fun@54bcd848d716
permissions -rw-r--r--
updatedHG: added solutions/cw5/fun_tokens.sc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Towers of Hanoi in Fun
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
def hanoi(n: Int, a: Int, b: Int, c: Int) : Void =
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
  if n != 0 then {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
    hanoi(n - 1, a, c, b);
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
    print_int(a);
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
    print_char('-'); print_char('>'); // prints out "->"
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
    print_int(b);
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
    print_char('\n');
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
    hanoi(n - 1, c, b, a)
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
  } else skip;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
867
54bcd848d716 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 864
diff changeset
    13
hanoi(4,1,2,3)