next up previous contents index
Next: Defining New Functions (function) Up: Functions and Constructs Previous: Binding Local Variables (let)

The Apply-to-Each Construct ({})

   

The apply-to-each construct is used to apply any function over the elements of a sequence. It has the following syntax:

tabular4335

An apply-to-each construct consists of three parts: the expression before the colon, which we will call the body, the bindings that follow the body, and the expression that follows the |, which we will call the sieve. Both the body and the sieve are optional: they could both be left out, as in

The rbinds can contain multiple bindings which are separated by semicolons. We first consider the case in which there is a single binding. A binding can either consist of a pattern followed by the keyword IN and an expression (full binding), or consist of a variable name (shorthand binding). In a full binding the expression is evaluated (it must evaluate to a sequence) and the variables in the pattern are bound in turn to each element of the sequence. The body and sieve are applied for each of these bindings. For example:

    tabular6072

In a shorthand binding, the variable must be a sequence, and the body and sieve are applied to each element of the sequence with the variable name bound to the element. For example:

    tabular6091

In the case of multiple rbinds, each of the sequences (either the result of the expression in a full binding or the value of the variable in a shorthand binding) must be of equal length. The bindings are interleaved so that the body is evaluated with bindings made for elements at the same index of each sequence. For example:

    tabular6111

    tabular6130

An apply-to-each with a body and two bindings,

     {body: pattern1 in exp1; pattern2 in exp2 | sieve}  
    

is equivalent to the single binding construct

     {body: (pattern1,pattern2) in zip(exp1,exp2) | sieve}  
    

where zip, as defined in the list of functions, elementwise zips together the two sequences it is given as arguments.

If there is no body in an apply-to-each construct, then the results of the first binding is returned. For example:

    tabular6173

    tabular6192

    tabular6211

If there is a body and a sieve, the body and sieve are both evaluated for all bindings, and then the subselection is applied. An apply-to-each with a sieve of the form:

     {body : bindings | sieve}  
    

is equivalent to the construct

     pack({(body,sieve) : bindings})  
    
where pack, as defined in the list of functions, takes a sequence of type [(alpha,bool)] and returns a sequence which contains the first element of each pair if the second element is true. The order of remaining elements is maintained.



next up previous contents index
Next: Defining New Functions (function) Up: Functions and Constructs Previous: Binding Local Variables (let)



Jonathan Hardwick
Tue Nov 28 13:57:00 EST 1995