cwtests/cw05/fact2.fun
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 21 Feb 2024 09:14:12 +0000
changeset 959 64ec1884d860
parent 856 cwtests/cw05/fact.fun@23273e3a120f
permissions -rw-r--r--
updated and added pascal.while file

// a simple factorial program
// (including a tail recursive version)


def fact(n: Int) : Int =
  if n == 0 then 1 else n * fact(n - 1);

def facT(n: Int, acc: Int) : Int =
  if n == 0 then acc else facT(n - 1, n * acc);

def facTi(n: Int) : Int = facT(n, 1);

def top() : Void = {
  print_int(fact(6));
  print_char(',');
  print_int(facTi(6));
  print_char('\n')
};

top()