progs/fun/fact.fun
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 17 Nov 2023 20:06:43 +0000
changeset 955 47acfd7f9096
parent 814 1fbaa5f05516
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) =
735
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 702
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
814
1fbaa5f05516 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 735
diff changeset
    12
write(fact(6)); facTi(6)
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    13
735
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 702
diff changeset
    14
// a simple factorial program
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 702
diff changeset
    15
// (including a tail recursive version)