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-- |
655 | 1 |
def fact(n) = |
735
fc2e3609d5e5
updatd
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
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 |
|
735
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
702
diff
changeset
|
14 |
// a simple factorial program |
fc2e3609d5e5
updatd
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
702
diff
changeset
|
15 |
// (including a tail recursive version) |