next up previous contents
Next: Using non-procedures (e.g. Up: CHAPTER.4: PROCEDURES IN Previous: Using both partial

Exercises on procedure creation and manipulation

1. Using the notation defined above, use procedure composition and partial application to create a procedure called octuple_elements which, when applied to a list of numbers, creates a new list containing the results of multiplying those numbers by 8, i.e. the double of the double of the double of each number.

2. Note that the built-in Pop-11 procedure maplist can be used instead of list_results, in most of the examples. Check this by changing all the examples to use maplist instead, and see how they work. If you give a non-procedure as the second argument of maplist an error will result.

3. Use procedure composition and partial application to combine the procedures maplist double and sqrt to produce a new procedure called list_sqrt_doubles that when applied to a list of numbers produces the square roots of the doubles of those numbers, and test it thus:

    list_sqrt_doubles([2 8 12.5]) =>
It should produce

    ** [2.0 4.0 5.0]
Just to test your understanding, in preparation for the next section, try to work out what the following do, remembering that a vector, or list can be used as a procedure by applying it to a number. So when a list or vector of objects is given as second argument to maplist, an error results, whereas list_results is more generous! For example:

    list_results([3 4], {cat dog mouse bird rabbit}) =>
    ** [mouse bird]

    list_results([1 3], [a b c d e]) =>
    ** [a c]
What will this one produce?

    list_results([2 4], [[list 1] [list 2] [list 3] [list 4]]) =>
What will be printed out as a result of these instructions:

    vars colourwords = [red orange yellow green blue indigo violet];

    define select_colours = list_results(% colourwords %) enddefine;

    select_colours([2 4]) =>

    select_colours([1 3 5]) =>
What does it mean to say that a programming language treats procedures as first class objects? Why is this important?

7. What sorts of things can Pop-11 programs do with procedures besides calling them?

8. In what ways can Pop-11 programs create new procedures while they are running? How does the useof popval differ from the others.



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