3 min read, 721 words
In Perpetuum
Indeed, loops, as the name may suggest, perform a certain task β that which is placed within them β for a certain duration; one generally assigns said duration to the loop as a mathematical condition, for example: so long as statement x returns the value true, execute the following code. This type of loop is titled a while loop.
In addition to while loops, another type of loop, namely the for loop, is also to be found. Therein you define a variable within the head of the function, add a condition and, lastly, provide an operation that is to be conducted upon the priorly defined variable. For example, one may construct a for loop in whose head the variable x is defined as an integer with a value of 0; thereafter specify that the loop is to be run as long as x is less than 10; and thereupon instruct the loop to increment the value of x by 1. This would result in the code placed within the functions body to be executed ten times. Such looks thusly in Limbo: β
# for_loop.b
# ...
init (ctxt: ref Draw->Context, args: list of string) {
sys = load Sys Sys->PATH;
for (i := 0; i < 10; i++)
sys->print("%d, ", i);
}
This program would return the numbers 0 through 9 separated by a comma.
As heretofore discussed, the arguments appended to our program are of the type list of string; this allows us to iterate over each entry, store it in a regular string and print the results β printing lists on their own is, unfortunately, irrealisable. Let us thus improve our greet program even further, by using our newly acquired knowledge of loops so that a user may append more than one name: β
# loop_arg.b
implement GreetEvenMoreImproved;
include "sys.m";
include "draw.m";
sys: Sys;
GreetEvenMoreImproved: module {
init: fn (ctxt: ref Draw->Context, args: list of string);
};
init(ctxt: ref Draw->Context, args: list of string) {
sys = load Sys Sys->PATH;
args = tl args;
for (s := ""; args != nil; args = tl args)
s += " " + hd args;
if (s != "")
sys->print("Hello %s \n", s[1:]);
if (s == "")
sys->print("Enter your name.\n");
}
We shall commence by briefly glancing at line 13 of this program which contains the following line.
args = tl args;
Lists can be addressed using the tl β tail β and hd β head β commands, wherein the head returns the first argument as a string, and tail returns all other arguments as a list of string. Thus, on this line, we effectively remove the head from the args variable β and, as I hope you remember, since the first argument is the name of the program itself, it is not needed anyhow.
The remaining lines consist of the for loop itself and two if statements, whereof the latter should be simple enough to grasp quickly and we shall thus concentrate on lines 14 and 15 primarily: β
for (s := ""; args != nil; args = tl args) s += " " + hd args;
The for loopβs initial statement is the declaration of the s variable as an empty string; thereafter follows the condition of args not equalling nil; and lastly the command args = tl args is supposed to be executed, videlicet discarding the head of the args variable; this is to be done until args no longer contains any data β id est when its value has become nil.
The successive line then attaches a space β " " β onto the s variable, plucks the head from the args variable and, lastly, adjoins it to the s variable; thereupon the loop is executed again, yet this time the args variable has had one of its arguments expunged.