progs/fun-tests/fact.fun
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 25 Oct 2024 18:54:08 +0100
changeset 971 51e00f223792
parent 790 31a9f89776a3
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
     1
def fact(n) =
790
31a9f89776a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
     2
  (if n == 0 then 1 else n * fact(n - 1));
224
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
     4
def facT(n, acc) =
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
     5
  if n == 0 then acc else facT(n - 1, n * acc);
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
     6
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
     7
def facTi(n) = facT(n, 1);
224
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
     9
//fact(10)
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    10
//facTi(10)
224
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
702
39e21a33ffb0 updated
Christian Urban <urbanc@in.tum.de>
parents: 655
diff changeset
    12
write(facTi(6))
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    13
790
31a9f89776a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    14
// a simple factorial program
31a9f89776a3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 742
diff changeset
    15
// (including a tail recursive version)