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-- |
655 | 1 |
def fact(n) = |
790 | 2 |
(if n == 0 then 1 else n * fact(n - 1)); |
224
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
|
655 | 4 |
def facT(n, acc) = |
5 |
if n == 0 then acc else facT(n - 1, n * acc); |
|
6 |
||
7 |
def facTi(n) = facT(n, 1); |
|
224
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
|
655 | 9 |
//fact(10) |
10 |
//facTi(10) |
|
224
70198792c2aa
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
|
702 | 12 |
write(facTi(6)) |
655 | 13 |
|
790 | 14 |
// a simple factorial program |
15 |
// (including a tail recursive version) |