From 5bcfcb773f9153df55fb6d344ef791b19383a1b6 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Sat, 12 Feb 2022 13:17:11 +0100 Subject: [PATCH] add some example programs --- examples/fib.par | 16 ++++++++++++++++ examples/gcd.par | 6 ++++++ examples/sqrt.par | 9 +++++++++ 3 files changed, 31 insertions(+) create mode 100644 examples/fib.par create mode 100644 examples/gcd.par create mode 100644 examples/sqrt.par diff --git a/examples/fib.par b/examples/fib.par new file mode 100644 index 0000000..1383362 --- /dev/null +++ b/examples/fib.par @@ -0,0 +1,16 @@ +fn fib[n: int]: int { + if n < 2 { + return n; + } + + var x0 = 0; + var x1 = 1; + while n > 0 { + var tmp = x0 + x1; + x0 = x1; + x1 = tmp; + n = n - 1; + } + + return x1; +} diff --git a/examples/gcd.par b/examples/gcd.par new file mode 100644 index 0000000..cb97ee4 --- /dev/null +++ b/examples/gcd.par @@ -0,0 +1,6 @@ +fn gcd[a: int, b: int]: int { + if b == 0 { + return a; + } + return gcd[b, a % b]; +} diff --git a/examples/sqrt.par b/examples/sqrt.par new file mode 100644 index 0000000..2fe1df8 --- /dev/null +++ b/examples/sqrt.par @@ -0,0 +1,9 @@ +fn sqrt[a: float]: float { + var x = 0.5 * a; + var i = 10; + while i > 0 { + x = 0.5 * (x + a / x); + i = i - 1; + } + return x; +}