fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.
Fuzion
•
Idioms
•
Idiom # 301: Recursive Fibonacci sequence
Idiom # 301: Recursive Fibonacci sequence
See
programming-idioms.org
:
Naive approach
Code
fib(n i32) i32 => if n<2 then n else (fib (n-1)) + (fib (n-2))
What are effects?
Runnable Example
ex301 is fib(n i32) i32 pre n >= 0 => if n<2 then n else (fib (n-1)) + (fib (n-2)) for i in 0..10 do say "fib $i = {fib i}"
What are effects?
Efficient tail recursive implementation
ex301a is fib(n i32) pre n >= 0 => fib_acc(n1, n2, n0 i32) => if n0=0 then n1 else fib_acc n2 (n1+n2) (n0-1) fib_acc 0 1 n for i in 0..10 do say "fib $i = {fib i}"
What are effects?
last changed: 2024-07-01
next: Idiom # 302: String interpolation