25 lines
704 B
Plaintext
25 lines
704 B
Plaintext
import std::io;
|
|||
|
|
def twice<T>(x: T): T where T: numeric {
|
||
|
|
def add(a: T): T { return a + x; }
|
||
|
|
return add(x);
|
||
|
|
}
|
||
|
|
def main(): void {
|
||
|
|
var calls := 0;
|
||
|
|
def fib(n: int): int {
|
||
|
|
calls++;
|
||
|
|
return n < 2 ? n : fib(n - 1) + fib(n - 2);
|
||
|
|
}
|
||
|
|
println($"{fib(15)} {calls}");
|
||
|
|
# A local function refers to the locals it uses, so one using `i` is called
|
||
|
|
# while `i` is in scope.
|
||
|
|
var made: int[3];
|
||
|
|
for i in 0..3 {
|
||
|
|
def scaled(x: int): int { return x * i + fib(i); }
|
||
|
|
made[i] = scaled(10);
|
||
|
|
}
|
||
|
|
def outer(n: int): int {
|
||
|
|
def inner(m: int): int { return m <= 0 ? 0 : 1 + outer(m - 1); }
|
||
|
|
return inner(n);
|
||
|
|
}
|
||
|
|
println($"{made[0]} {made[1]} {made[2]} {outer(5)} {twice(4)} {twice(1.5)}");
|
||
|
|
}
|