solutions/cw5/fact.fun
changeset 894 02ef5c3abc51
parent 864 b5b1bc0a603b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/solutions/cw5/fact.fun	Fri Nov 04 12:07:40 2022 +0000
@@ -0,0 +1,21 @@
+// 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()
+