diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/if.lyn | 15 | ||||
-rw-r--r-- | examples/sum.lyn | 18 |
2 files changed, 33 insertions, 0 deletions
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) |