changeset 894 | 02ef5c3abc51 |
parent 864 | b5b1bc0a603b |
893:54a483a33763 | 894:02ef5c3abc51 |
---|---|
1 // a simple factorial program |
|
2 // (including a tail recursive version) |
|
3 |
|
4 |
|
5 def fact(n: Int) : Int = |
|
6 if n == 0 then 1 else n * fact(n - 1); |
|
7 |
|
8 def facT(n: Int, acc: Int) : Int = |
|
9 if n == 0 then acc else facT(n - 1, n * acc); |
|
10 |
|
11 def facTi(n: Int) : Int = facT(n, 1); |
|
12 |
|
13 def top() : Void = { |
|
14 print_int(fact(6)); |
|
15 print_char(','); |
|
16 print_int(facTi(6)); |
|
17 print_char('\n') |
|
18 }; |
|
19 |
|
20 top() |
|
21 |