diff options
| author | Cody <cody@codyq.dev> | 2023-02-27 07:21:50 -0600 |
|---|---|---|
| committer | Cody <cody@codyq.dev> | 2023-02-27 07:21:50 -0600 |
| commit | ebfd74ddf0ef6372624ea171e06f8460d0e1351b (patch) | |
| tree | d59df0e9600a6f2b86b57949048cf990a3e102ae /examples/fib.sloth | |
| parent | a653a6602fe5ae5eb4739755db7b34bc92ecdadf (diff) | |
| download | sloth-ebfd74ddf0ef6372624ea171e06f8460d0e1351b.tar.gz | |
pain
Diffstat (limited to 'examples/fib.sloth')
| -rw-r--r-- | examples/fib.sloth | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/examples/fib.sloth b/examples/fib.sloth index 415de05..e98764a 100644 --- a/examples/fib.sloth +++ b/examples/fib.sloth @@ -1,15 +1,24 @@ -## Calculate a specific number in the fibonacci sequence fn fib(n: i32) -> i32 { - match n { - 0 | 1 => n, - _ => fib(n - 1) + fib(n - 2), + if n == 0 || n == 1 { + return n; } -} -generator fn fib_sequence(range: Range<i32>) -> i32 { - for n in range { - yield fib(n); + var grandparent = 0; + var parent = 1; + var me = 0; + + for i in 0..n-1 { + me = parent + grandparent; + grandparent = parent; + parent = me; } + + return me; } -print(fib_sequence(0..20).join(", ")) +print fib(0); +print fib(1); +print fib(2); +print fib(3); +print fib(4); +print fib(5); |
