progs/fun/fact.fun
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Mon, 20 Jul 2020 10:06:43 +0100
changeset 735 fc2e3609d5e5
parent 702 progs/fact.fun@39e21a33ffb0
child 814 1fbaa5f05516
permissions -rw-r--r--
updatd
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
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
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)