aboutsummaryrefslogtreecommitdiff
path: root/examples/fib.sloth
diff options
context:
space:
mode:
authorCody <cody@codyq.dev>2023-02-27 07:21:50 -0600
committerCody <cody@codyq.dev>2023-02-27 07:21:50 -0600
commitebfd74ddf0ef6372624ea171e06f8460d0e1351b (patch)
treed59df0e9600a6f2b86b57949048cf990a3e102ae /examples/fib.sloth
parenta653a6602fe5ae5eb4739755db7b34bc92ecdadf (diff)
downloadsloth-ebfd74ddf0ef6372624ea171e06f8460d0e1351b.tar.gz
pain
Diffstat (limited to 'examples/fib.sloth')
-rw-r--r--examples/fib.sloth27
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);