From 18262dcbecd97591dd15ee9274a81abb8c2ba1c4 Mon Sep 17 00:00:00 2001 From: Kimplul Date: Tue, 15 Oct 2024 22:26:00 +0300 Subject: initial musings --- examples/if.lyn | 15 +++++++++++++++ examples/sum.lyn | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 examples/if.lyn create mode 100644 examples/sum.lyn (limited to 'examples') diff --git a/examples/if.lyn b/examples/if.lyn new file mode 100644 index 0000000..294a490 --- /dev/null +++ b/examples/if.lyn @@ -0,0 +1,15 @@ +# `if` macro, think of it as a statement I guess? +# else is used by the `if` command in C, presumably? +if {< i 10} { + println "a" +} {< i 20} { + println "b" +} else { + println "c" +} + +# `if` procedure, think of it as expr-if in other languages I guess +# note that all arguments get evaluated here, so if you have a slow function in +# one branch you should probably prefer `if` statements + +let x (do-if (< i 10) "a" (< i 20) "b" else "c") diff --git a/examples/sum.lyn b/examples/sum.lyn new file mode 100644 index 0000000..a4dcf61 --- /dev/null +++ b/examples/sum.lyn @@ -0,0 +1,18 @@ +syntax for {init cond post body} { + eval init + while (eval cond) { + eval body + eval post + } +} + +def sum {n} { + require n int + let s 0 + for {let i 0} {< i n} {set i (+ i 1)} { + set s (+ s i) + } + return s +} + +println (sum 1000000) -- cgit v1.2.3