blob: b0a416c5f5b26c3959c0185dad4ba5c0c05c2e09 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import gleam/io
pub fn main() {
io.debug(factorial(5))
io.debug(factorial(7))
}
pub fn factorial(x: Int) -> Int {
// The public function calls the private tail recursive function
factorial_loop(x, 1)
}
fn factorial_loop(x: Int, accumulator: Int) -> Int {
case x {
0 -> accumulator
1 -> accumulator
// The last thing this function does is call itself
// In the previous lesson the last thing it did was multiply two ints
_ -> factorial_loop(x - 1, accumulator * x)
}
}
|