A template creates a query solution in a sequential and orderly manner, like an assembly line. A structure called a "flow" is a template that allows it's terms to divert the flow of execution away from the body to perform some function and return. This gives rise to the expression "sideways execution" as it contrasts with the normal top-down sequencing of a template. The term "flow" conveys the concept of continuous progression from start to finish. All execution advances one term at a time.
Loop
As an example of sideways execution, imagine how the cursor introduced in personalities2.taq can take advantage of a "loop" template that executes in a loop until a criterion causes it to short circuit. The compiler can check that the loop contains at least one criterion to avoid an infinite loop. The cursor code can change from
to
loop
(
personality += traits++ + " "
The immediate advantage of loop over the original code is that, if by chance the "traits" list is empty, this will be dealt with gracefully. Better still, the number of items in the list no longer has to be the same in each case.
For loop to be a success it must have access to all the variables declared above in the host template and participate in unification just like it's host. The loop integrates with the host template so closely that a minimal format has been decided on - that tis o simply enclose the template terms in braces. Hence the template transformed into a flow looks like this
{ cursor<string> traits }
(
age,
starsign,
string personality = "",
{
personality += traits++ + " "
Note that the loop in the above flow has no meaningful value to pass to it's solution. As a term, a loop is always private.
Factorial Calculator
factorial.taq, using a loop, generates the factorial of 4 (2*3*4 = 24) and 5 (2*3*4*5 = 120). The task of calculating the factorial is given to a loop which iterates a counter until the factorial number is reached:
{ integer i }
(
factorial = 1,
{ ?? (i++ < n) factorial *= i }
query<term> factorial4 (factorial)(n = 4)
query<term> factorial5 (factorial)(n = 5)
Insert Sort
high-cities-sorted.taq sorts a list of cities by altitude and has a loop to shuffle cities into sort order. This loop has 2 criterions, one to find the the right place to insert the next city based on altitude and a second to check for the start of the list while navigating it in reverse.
{
high_cities[i + 1] = high_cities[i],
? --i >= 0