Fundamentals of CS I (CS151 2001S)

[Current]
[Discussions]
[Glance]
[Honesty]
[Instructions]
[Links]
[News]
[Search]
[Syllabus]
**Primary**

[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Quizzes]
[Readings]
[Reference]
**Sets**

[Blackboard]
[Scheme Report]
[SamR's Schedule]
[Rebelsky/Fall 2000]
[Walker/Fall2000]
[Stone/Spring2000]
**Links**

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

b. Start DrScheme

`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.

Use `apply`

and `map`

to concisely define a
procedure, `(dot-product `

, that
takes as arguments two lists of numbers, equal in length, and returns
the sum of the products of corresponding elements of the arguments:
*list1* *list2*)

>(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

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?

a. Document and write a procedure, `(tally `

,
that counts the number of values in *predicate*
*list*)*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.

Document and write a procedure, `(make-tallier `

,
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
*predicate*)

>(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.

Thursday, 2 November 2000 [Samuel A. Rebelsky]

- Created. Problems generally taken from elsewhere and reworded slightly.
- This version available at
`http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2000F/Labs/hop.html`

Wednesday, 14 February 2001 [Samuel A. Rebelsky]

- Moved many problems into a new lab so that there are now two higher-order procedures labs.
- Added new problems (1, 4, and 5).
- Removed unused notes section.

Sunday, 8 April 2001 [Samuel A. Rebelsky]

- Fixed a small formatting problem.
- This version available at
`http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2001S/Labs/higher-order.html`

[Current]
[Discussions]
[Glance]
[Honesty]
[Instructions]
[Links]
[News]
[Search]
[Syllabus]
**Primary**

[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Quizzes]
[Readings]
[Reference]
**Sets**

[Blackboard]
[Scheme Report]
[SamR's Schedule]
[Rebelsky/Fall 2000]
[Walker/Fall2000]
[Stone/Spring2000]
**Links**

**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.