progs/fact.fun
author Christian Urban <urbanc@in.tum.de>
Thu, 14 Nov 2019 13:17:02 +0000
changeset 687 8865f4f2be59
parent 655 3d04ee04966d
child 702 39e21a33ffb0
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
// a simple factorial program
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
     2
// (including a tail recursive version)
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
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
     5
def fact(n) =
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
     6
  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
     7
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
     8
def facT(n, acc) =
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
     9
  if n == 0 then acc else facT(n - 1, n * acc);
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    10
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    11
def facTi(n) = facT(n, 1);
224
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    13
//fact(10)
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    14
//facTi(10)
224
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
655
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    16
write(facTi(5 + 5))
3d04ee04966d updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    17