aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/fib.sloth15
-rw-r--r--examples/hello.sloth6
2 files changed, 21 insertions, 0 deletions
diff --git a/examples/fib.sloth b/examples/fib.sloth
new file mode 100644
index 0000000..415de05
--- /dev/null
+++ b/examples/fib.sloth
@@ -0,0 +1,15 @@
+## Calculate a specific number in the fibonacci sequence
+fn fib(n: i32) -> i32 {
+ match n {
+ 0 | 1 => n,
+ _ => fib(n - 1) + fib(n - 2),
+ }
+}
+
+generator fn fib_sequence(range: Range<i32>) -> i32 {
+ for n in range {
+ yield fib(n);
+ }
+}
+
+print(fib_sequence(0..20).join(", "))
diff --git a/examples/hello.sloth b/examples/hello.sloth
new file mode 100644
index 0000000..3dbc685
--- /dev/null
+++ b/examples/hello.sloth
@@ -0,0 +1,6 @@
+print("Hello World!");
+
+## A basic for loop greeting the user in multiple languages
+for greeting in ["Hello", "Hola", "你好"] {
+ print(greeting + " World!");
+}