diff options
author | Joel Kronqvist <joel.kronqvist@iki.fi> | 2025-08-23 10:34:29 +0300 |
---|---|---|
committer | Joel Kronqvist <joel.kronqvist@iki.fi> | 2025-08-23 10:34:29 +0300 |
commit | 485cf739e241732a167920eb0d9af0d53c0facb7 (patch) | |
tree | 285d35cc935772d34a9e7debf3ea2534594566d8 | |
parent | 9a9f78a8dab77f476f82eb218c3d71aef55f13ec (diff) | |
download | myslip-485cf739e241732a167920eb0d9af0d53c0facb7.tar.gz myslip-485cf739e241732a167920eb0d9af0d53c0facb7.zip |
doc: repeat in TUTORIAL.md
-rw-r--r-- | TUTORIAL.md | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/TUTORIAL.md b/TUTORIAL.md index ba6b8a1..df2eea7 100644 --- a/TUTORIAL.md +++ b/TUTORIAL.md @@ -82,6 +82,12 @@ form `(if [condition] [iftrue] [iffalse])`: > (if false 1 0) 0 : Int ``` +Note though that they are not short-circuiting as their +arguments are evaluated before passing to the operator as +with all functions defined in myslip. Implementing a +short-circuiting version would be possible if an 'unquoting' +operator was added. If you wish short-circuiting behavior, +use case directly instead. Integer comparisons @@ -416,6 +422,43 @@ print twice looks horrible. That looks better! +Loops +===== + +Loops are quite worthless in a functional language, but I +might get points for them. The standard library provides the +functions `repeat` and `map-i->i`. + +The former one has the syntax +`(repeat [operation] [number of times])` which quite +intuitively repeats the given operation the given number of +times. The operation should be a function that takes an +integer as an argument and returns Nil, so this function is +quite useless for anything but printing. +```console +> (repeat (fn x Int Nil (print x)) 10) +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +() : Nil +``` + +The latter has the syntax `(map-i->i [vector] [operation])` +where only vectors of integers and operations from integers +to integers are supported. More general mapping functions +would require more sophisticated generics. + +The standard library also provides a similar function for +filtering on vectors of integers. + + Exercises ========= |