diff options
| author | Baitinq <[email protected]> | 2025-03-09 12:12:10 +0100 |
|---|---|---|
| committer | Baitinq <[email protected]> | 2025-03-09 12:12:10 +0100 |
| commit | 13009c9d92756ecbf38d2ac9b9e85a59c92fc8b9 (patch) | |
| tree | 594184a7181b6604015108521b774235f0f1ab71 /examples/13.src | |
| parent | Codegen: Fix bug with if statements inside while loops (diff) | |
| download | pry-lang-13009c9d92756ecbf38d2ac9b9e85a59c92fc8b9.tar.gz pry-lang-13009c9d92756ecbf38d2ac9b9e85a59c92fc8b9.tar.bz2 pry-lang-13009c9d92756ecbf38d2ac9b9e85a59c92fc8b9.zip | |
Examples: Add new more complex examples
Diffstat (limited to 'examples/13.src')
| -rw-r--r-- | examples/13.src | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/examples/13.src b/examples/13.src new file mode 100644 index 0000000..eeb5b32 --- /dev/null +++ b/examples/13.src @@ -0,0 +1,31 @@ +let main = () => i64 { + /* Iterative Fibonacci using while loop. */ + let fibonacci_iter = (n: i64) => i64 { + let a = 0; + let b = 1; + let i = 0; + while i < n { + let temp = b; + b = a + b; + a = temp; + i = i + 1; + }; + return a; + }; + + /* Recursive GCD using Euclid's algorithm. */ + let gcd = (a: i64, b: i64) => i64 { + if b == 0 { + return a; + }; + return gcd(b, a % b); + }; + + let fib_val = fibonacci_iter(10); + print(fib_val); + + let gcd_val = gcd(48, 18); + print(gcd_val); + + return 0; +}; |