-
Notifications
You must be signed in to change notification settings - Fork 1
Loops
Viliam Vadocz edited this page Aug 13, 2020
·
1 revision
Loops are quite common, but the standard way of making them is a bit difficult to read because the initial value is at the end while arguments are at the top.
(i) {
exit_condition -> result
...
@(i + 1)
} (0)
Often times we might want to modify something in the loop, but because it is a function, we have to pass it along:
(i, x) {
exit_condition -> x
...
@(i + 1, x)
} (0, initial_x)
Have the initial values next to the arguments
(i := 0, x := initial_x) { ... }
Create new loop syntax. This is not a new scope, so things we want to modify do not have to be passed along
> var := start
| some stuff
* exit condition
| these
| don't
| loop
^ conditionally loop back
| neither
| this
^ unconditionally loop back
example (uses unimplemented array initialisation syntax):
fib := (n) {
arr := [1, 1; n + 1]
> i := 2
| i > n -> arr[n]
| arr[i] := arr[i - 1] + arr[i - 2]
^ i + 1
}