Recursion

Inside a function declaration, we can refer to the function itself using the self keyword. This allows us to write recursive functions. A recursive function to calculate the fibonacci numbers can be written using the func keyword and a match block for handling the anchor cases.

fib = func(n int) int { match n { 0 -> 0, 1 -> 1, _ -> self(n - 1) + self(n - 2) } } fib(20)

© 2024 ouzu