ginger/examples/fib.gg

25 lines
760 B
Plaintext
Raw Normal View History

* A function which accepts a number N and returns the Nth fibonacci number
{
* We are passing a tuple of inputs into a graph here, such that the graph is
2023-10-29 20:42:41 +00:00
* evaluated as an anonymous function. That anonymous function uses !recur
* internally to compute the result.
2023-10-29 20:42:41 +00:00
!out = {
2021-12-30 22:29:38 +00:00
* A little helper function.
2023-10-29 20:42:41 +00:00
decr = { !out = !add < (!in, -1) };
2021-12-30 22:29:38 +00:00
* Deconstruct the input tuple into its individual elements, for clarity.
* There will be a more ergonomic way of doing this one day.
2023-10-29 20:42:41 +00:00
n = !tupEl < (!in, 0);
a = !tupEl < (!in, 1);
b = !tupEl < (!in, 2);
2021-12-30 22:29:38 +00:00
2023-10-29 20:42:41 +00:00
!out = !if < (
!isZero < n,
a,
2023-10-29 20:42:41 +00:00
!recur < ( decr<n, b, !add<(a,b) ),
2021-12-30 22:29:38 +00:00
);
2023-10-29 20:42:41 +00:00
} < (!in, 0, 1);
}