Renamed loops

This commit is contained in:
Benjamin Vedder 2022-03-26 16:42:08 +01:00
parent 76b12e0599
commit 55405056e2
2 changed files with 12 additions and 12 deletions

View File

@ -941,16 +941,16 @@ Read state of pin. Returns 1 if the pin is high, 0 otherwise.
In general it is encouraged to do iteration by recursion, but there are a few loop constructs available for convenience.
#### loop-for
#### loopfor
```clj
(loop-for it start cond update body)
(loopfor it start cond update body)
```
For-loop. it is the iterator, start is what it is initialized to, cond is the condition that has the be true for the loop to continue running, update is how to update the iterator after each iteration and body is the code to execute each iteration. The iterator can be accessed from within body. Example:
```clj
(loop-for i 0 (< i 5) (+ i 1)
(loopfor i 0 (< i 5) (+ i 1)
(progn
(print i)
(sleep 0.5)
@ -964,10 +964,10 @@ Output:
4
```
#### loop-while
#### loopwhile
```clj
(loop-while cond body)
(loopwhile cond body)
```
While-loop. cond is the condition that has the be true for the loop to continue running and body is the code to execute each iteration. Example:
@ -975,7 +975,7 @@ While-loop. cond is the condition that has the be true for the loop to continue
```clj
(define i 0)
(loop-while (< i 5)
(loopwhile (< i 5)
(progn
(print i)
(sleep 0.5)
@ -990,16 +990,16 @@ Output:
4
```
#### loop-range
#### looprange
```clj
(loop-range it start end body)
(looprange it start end body)
```
Range-loop. Iterate it from start to end and evaluate body for each iteration. The iterator can be accessed from within body. Example:
```clj
(loop-range i 0 5
(looprange i 0 5
(progn
(print i)
(sleep 0.5)

View File

@ -100,15 +100,15 @@ static const char* functions[] = {
static const char* macros[] = {
"(define defun (macro (name args body) `(define ,name (lambda ,args ,body))))",
"(define loop-for (macro (it start cond update body)"
"(define loopfor (macro (it start cond update body)"
"`(let ((loop (lambda (,it res)(if ,cond (loop ,update ,body) res"
"))))(loop ,start nil))))",
"(define loop-while (macro (cond body)"
"(define loopwhile (macro (cond body)"
"`(let ((loop (lambda (res)(if ,cond (loop ,body)res"
"))))(loop nil))))",
"(define loop-range (macro (it start end body)"
"(define looprange (macro (it start end body)"
"`(let ((loop (lambda (,it res)(if (< ,it ,end)(loop (+ ,it 1),body)res"
"))))(loop ,start nil))))",
};