int fibonacci(int n) {
if (n < 2) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
print(fibonacci(10));
In egg, functions defined like this are actually special cases of callable objects. The script declares an identifier "fibonacci" which is initialised with an instance of an object that supports the "call" operation: in this case, taking a single integer parameter and returning an integer.
At present, there is no notion of read-only variables in egg, so it's possible to subsequently assign a different function to "fibonacci":
int fibonacci(int n) {
if (n < 2) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
int zero(int n) {
return 0;
}
fibonacci = zero;
print(fibonacci(10));
No comments:
Post a Comment