Lab: Composition and pipelining

Assigned
Friday, 15 September 2023
Summary
While lambda expressions are the most common way to write procedures, there are also a variety of others. We consider how to use composition and pipelining to build new procedure from old.

Useful procedures and notation

Creating lists

(list exp1 exp2 ... expn) - create a list by evaluating each of the expressions and then joining together their values.

(make-list n val) - make a list of n copies of val.

(range n) - create a list of all the natural numbers strictly less than n (starting with 0).

(range s n) - create a list of all the natural numbers between m (inclusive) and n (exclusive).

(range s n i) - create a list of all the natural numbers between m (inclusive) and n (exclusive), incrementing by i each time.

Manipulating lists

(apply fun lst) - apply the function to all the elements of the list, en masse.

(filter pred? lst) - Select only the elements of the list for which the predicate holds.

(map fun lst) - apply the function to each element of the list. (map fun (list val1 val2 ... valn)) gives you (list (fun val1) (fun val2) ... (fun valn)).

(map fun lst1 lst2) - create a new list by applying the function to corresponding pairs of elements from the two lists. You can also use map with more than two lists.

(reduce binproc lst) - reduce a list to a single value

Other list operations

(length lst) - Determine how many elements are in a list.

(reverse lst) - Create a new list with the elements in the opposite order.

(append lst1 lst2) - Join two lists together.

(take lst n) - Build a new list consisting of the first n elements of lst.

(drop lst n) - Build a new list consisting of all but the first n elements of lst.

(list-ref lst n) - Extract element n of the list. (Remember that lists start with element 0.)

(index-of val lst) - Determine the position of val in lst. (It turns out the position is how many values need to be dropped from lst to reach val.)

Fun higher-order procedures

(o f1 f2 f3) - Create a new procedure that applys f3 to its one argument, then f2 to the result, then f1 to that result. Note that this takes as many as you you want.

(|> v f1 f2 f3 ...) - Takes value v and “pipes” it through f1, f2, f3, …, in-order, feeding the output of the previous function as input to the next function.

Preparation

a. If you have not done so already, you may want to open a separate tab or window in your browser for the various readings.

b. Introduce yourself to your partner. Describe your strengths and approaches to work.

c. Review the double-dagger problems with your partner.

d. Choose one person to be driver A; the other person is driver B.

e. Load the lab.

f. Get started.