diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2021-11-25 13:30:25 +0200 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2021-11-25 19:01:31 +0200 |
| commit | 3d68debb9b90875b91b86b935b8b311758d40747 (patch) | |
| tree | 03622f8a083a863e404bc50b919b7946b97ccee1 | |
| parent | 5a8684445c2799458aa1d5b8dfd1aa032016e2aa (diff) | |
Update sample code
| -rw-r--r-- | README.md | 3 | ||||
| -rw-r--r-- | samples/recursion.tunk | 17 | ||||
| -rw-r--r-- | samples/sample1.tunk | 15 | ||||
| -rw-r--r-- | samples/sample2.tunk | 1 | ||||
| -rw-r--r-- | samples/tuples.tunk | 28 |
5 files changed, 47 insertions, 17 deletions
@@ -17,7 +17,7 @@ Build the `tunk` interpreter with `cargo`: Run `tunk` on a source file: - target/release/tunk run samples/sample1.tunk + target/release/tunk run samples/recursion.tunk Run `tunk` REPL: @@ -26,6 +26,7 @@ Run `tunk` REPL: ## TODO - 𝛂-conversion to avoid variable name collisions in recursive functions +- anonymous function syntax (`\x y -> x`) - A pipeline operator (`argument |> function`) - A function composition operator (`inner >> outer`) - tail call recursion optimization diff --git a/samples/recursion.tunk b/samples/recursion.tunk new file mode 100644 index 0000000..1c5ed19 --- /dev/null +++ b/samples/recursion.tunk @@ -0,0 +1,17 @@ +# Recursively calculate triangular number of n +rec n = triangle (int.sub n 1); +triangle n = int.eq? n 1 + 1 + (int.add + n + (rec n)); + +triangle 500; + +# Recursively calculate factorial of n +factorial n = + int.eq? n 0 + 1 + (int.mul n (factorial (int.sub n 1))); + +factorial 20;
\ No newline at end of file diff --git a/samples/sample1.tunk b/samples/sample1.tunk deleted file mode 100644 index 26e934d..0000000 --- a/samples/sample1.tunk +++ /dev/null @@ -1,15 +0,0 @@ -rec n = triangle (int.sub n 1); -triangle n = int.eq? n 1 - 1 - (int.add - n - (rec n)); - -triangle 500; - -# factorial n = -# int.eq? n 0 -# 1 -# (int.mul n (factorial (int.sub n 1))); - -# factorial 20;
\ No newline at end of file diff --git a/samples/sample2.tunk b/samples/sample2.tunk deleted file mode 100644 index eedd45d..0000000 --- a/samples/sample2.tunk +++ /dev/null @@ -1 +0,0 @@ -lempi-numero = int.zero; diff --git a/samples/tuples.tunk b/samples/tuples.tunk new file mode 100644 index 0000000..4c7f0a4 --- /dev/null +++ b/samples/tuples.tunk @@ -0,0 +1,28 @@ +# Define a constructor for making a 2-tuple (a, b) +new-pair a b fn = fn a b; + +# Define selector functions for picking out an element from the 2-tuple +pair-first p = p true; +pair-second p = p false; + +# Test them out +my-pair = new-pair 1 2; +pair-first my-pair; +pair-second my-pair; + +# Define a constructor for making a 3-tuple (a, b, c) +new-triple a b c fn = fn a b c; + +# Define selector functions for picking out an element from the 3-tuple +select1 a b c = a; +select2 a b c = b; +select3 a b c = c; +triple-first t = t select1; +triple-second t = t select2; +triple-third t = t select3; + +# Test them out +my-triple = new-triple "a" "b" "c"; +triple-first my-triple; +triple-second my-triple; +triple-third my-triple; |
