next up previous contents
Next: Using both partial Up: CHAPTER.4: PROCEDURES IN Previous: Example: creating new

Using "partial application" to create a closure

Previously we saw how to define the procedure iscolourword by using partial application to combine member with a list. We can produce another closure, as follows:

If we often wanted to double all the numbers in a list, we could create a new procedure by partially applying list_results to double, and then re-using the new procedure as needed.

To do this we use the partial application brackets "(% ... %)" which have the effect NOT of running a procedure but of creating a NEW procedure that can be run later on with the data between the brackets.

If P is a procedure that requires three numbers to run, then P(% 3, 4 %) is a combination of P and two numbers, which can be run later on if the third number is given.

Similarly the procedure list_results can be partially applied to the procedure double, to create a new procedure that can be run later on if a list is given to be provided as the first argument of list_results. E.g.

    vars double_elements = list_results(% double %);
or, using a better, but equivalent syntax, which shows more clearly that we are defining a new procedure:

    define double_elements = list_results(% double %)
    enddefine;
This defines double_elements to be a procedure made from the procedure list_results by combining it with the procedure double as its "frozen argument".

We now have a new procedure:

    double_elements =>
    ** <procedure double_elements>
We can apply double_elements to lots of different lists:

    double_elements([11 22 33]) =>
    ** [22 44 66]

    double_elements([66.5 103.45 89  77.002 1000000]) =>
    ** [133.0 206.9 178 154.004 2000000]


Aaron Sloman
Fri Jan 2 03:17:44 GMT 1998