I’m going to post some example code snippets, like the following, written in POOL to demonstrate some of POOLs cool features and to have some test code when I’m writing the parser and the interpreter.
This code snippet eveluates the fibonacci sequence recursively, using the memoizing features of the special function type sef_function
to avoid on the usual drawbacks of recursive evaluation: the multiple calculation of the same value.
sef_function fibonacci(n){
fibonacci(n - 1) + fibonacci(n - 2)
}
fibonacci(0) = 0
fibonnaci(1) = 1
fibonacci(5) #-> 5
/* Call stack of fibonacci(5):
fibonacci(5)
-> fibonacci(4) + fibonacci(3)
-> (fibonacci(3) + fibonacci(2)) + fibonacci(3)
-> ((fibonacci(2) + fibonacci(1)) + fibonacci(2)) + fibonacci(3)
-> (((fibonacci(1) + fibonacci(0)) + fibonacci(1)) + fibonacci(2)) + fibonacci(3)
-> (((1 + 0) + 1) + fibonacci(2)) + fibonacci(3)
-> ((1 + 1) + 1) + fibonacci(3)
-> (2 + 1) + 2
-> 3 + 2
-> 5 */