Template Based Iteration

HC Template Based Iteration
killThread apply

Template Based Iteration



The procedures in this section are iteration tools based on the idea of a "template." This is a generalization of an instruction list or an expression list in which "slots" are provided for the tool to insert varying data. Four different forms of template can be used.

The most commonly used form for a template is "explicit-slot" form, or "question mark" form.

Example:

show map [? * ?] [2 3 4 5]		;[4 9 16 25]

In this example, the MAP tool evaluated the template [? * ?] repeatedly, with each of the members of the data list [2 3 4 5] substituted in turn for the question marks. The same value was used for every question mark in a given evaluation. Some tools allow for more than one datum to be substituted in parallel; in these cases the slots are indicated by ?1 for the first datum, ?2 for the second, and so on:

show (map [(word ?1 ?2 ?1)] [a b c] [d e f])		;[ada beb cfc]

If the template wishes to compute the datum number, the form (? 1) is equivalent to ?1, so (? ?1) means the datum whose number is given in datum number 1. Some tools allow additional slot designations, as shown in the individual descriptions.

The second form of template is the "named-procedure" form. If the template is a word rather than a list, it is taken as the name of a procedure. That procedure must accept a number of inputs equal to the number of parallel data slots provided by the tool; the procedure is applied to all of the available data in order. That is, if data ?1 through ?3 are available, the template "PROC is equivalent to [PROC ?1 ?2 ?3].

show (map "word [a b c] [d e f])		;[ad be cf]

to dotprod :a :b	; vector dot product
	op apply "sum (map "product :a :b)
end

The third form of template is "named-slot" or "lambda" form. This form is indicated by a template list containing more than one member, whose first member is itself a list. The first member is taken as a list of names; local variables are created with those names and given the available data in order as their values. The number of names must equal the number of available data. This form is needed primarily when one iteration tool must be used within the template list of another, and the ? notation would be ambiguous in the inner template.

Example:

to matmul :m1 :m2 [:tm2 transpose :m2]	; multiply two matrices
	output map [[row] map [[col] dotprod :row :col] :tm2] :m1
end

The fourth form is "procedure text" form, a variant of lambda form. In this form, the template list contains at least two members, all of which are lists. This is the form used by the define and Text primitives, and APPLY accepts it so that the text of a defined procedure can be used as a template.

Note: The fourth form of template is interpreted differently from the others, in that Logo considers it to be an independent defined procedure for the purposes of OUTPUT and stop.

For example, the following two instructions are identical:

show apply [[x] :x+3] [5]		;8
show apply [[x] [output :x+3]] [5]		;8

although the first instruction is in named-slot form and the second is in procedure-text form. The named-slot form can be understood as telling Logo to evaluate the expression :x+3 in place of the entire invocation of apply, with the variable x temporarily given the value 5. The procedure-text form can be understood as invoking the procedure

to foo :x
	output :x+3
end

with input 5, but without actually giving the procedure a name. If the use of OUTPUT were interchanged in these two examples, we'd get errors:

print apply [[x] output :x+3] [5]
;Can only use output inside a procedure
print apply [[x] [:x+3]] [5]		;8 ;-)

The named-slot form can be used with STOP or OUTPUT inside a procedure, to stop the enclosing procedure.

The following iteration tools are extended versions of the ones in

Appendix B of the book
Computer Science Logo Style, Volume 3:
Advanced Topics
by Brian Harvey [MIT Press, 1987].

The extensions are primarily to allow for variable numbers of inputs.

Template Based Iteration

Reference:

killThread apply