Fundamentals of CS I (CS151 2001S)

Higher-Order Procedures

Exercises

Exercise 0: Preparation

a. If you have not done so already, please scan the corresponding reading on higher-order procedures.

b. Start DrScheme

Exercise 1: Using map

a. Use map to compute the successors to the squares of the integers between 1 and 10.

b. Use map to turn a list into an association list by making each value in the first list into a a key (you can also use the same value as the corresponding value).

c. Use map to take the last element of each list in a list of lists. The result should be a list of the last elements.

d. Use apply and map to sum the last elements of each list in a list of lists of numbers. The result should be a number. You should have written a similar procedure for another lab.

Exercise 2: Dot-Product

Use apply and map to concisely define a procedure, (dot-product list1 list2), that takes as arguments two lists of numbers, equal in length, and returns the sum of the products of corresponding elements of the arguments:

> (dot-product (list 1 2 4 8) (list 11 5 7 3))
73
; ... because (1 x 11) + (2 x 5) + (4 x 7) + (8 x 3) = 11 + 10 + 28 + 24 = 73

> (dot-product null null)
0
; ... because in this case there are no products to add

Exercise 3: Why Apply?

Sarah and Steven Schemer suggest that ``apply is irrelevant. After all,'' they say, ``when you write

(apply prog (arg1 ... argn))

you're just doing the same thing as

(proc arg1 arg2 ... argn)

.''

Given your experience in the previous exercise, are they correct? Why or why not?

Exercise 4: Tallying

a. Document and write a procedure, (tally predicate list), that counts the number of values in list for which predicate holds.

b. Demonstrate the procedure by tallying the number of odd values in the list of the first twenty integers.

c. Demonstrate the procedure by tallying the number of multiples of three in the list of the first twenty integers.

Exercise 5: Making Talliers

Document and write a procedure, (make-tallier predicate), that builds a procedure that takes a list as a parameter and tallies the values in the list for which the predicate holds. For example

> (define count-odds (make-tallier odd?))
> (count-odds (list 1 2 3 4 5))
3

You can assume that tally already exists for the purpose of this problem.

 

History

Thursday, 2 November 2000 [Samuel A. Rebelsky]

Wednesday, 14 February 2001 [Samuel A. Rebelsky]

Sunday, 8 April 2001 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly. This means that they are rarely proofread and may contain bad grammar and incorrect details. It also means that I may update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This page was generated by Siteweaver on Thu May 3 23:07:46 2001.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2001S/higher-order.html.
You may validate this page's HTML.
The source was last modified Sun Apr 8 22:06:53 2001.