Fundamentals of Computer Science I: Media Computing (CS151.02 2007F)
[Skip to Body]
Primary:
[Front Door]
[Glance]

[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Assignment]
Groupings:
[Assignments]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151 20056F (Rebelsky)]
[CSC151.01 2007S (Davis)]
[CSCS151 2005S (Stone)]
This homework assignment is also available in PDF.
Assigned:
Due:
No extensions!
Summary: In this assignment, you will write your own higherorder procedure.
Purposes: To help you think about higherorder procedures and how they might help with control.
Expected Time: One to two hours.
Collaboration: You may work in a group of any size between one and four, inclusive. You may consult others outside your group, provided you cite those others. You need only submit one assignment per group.
Submitting: Email me your work, using a subject of CSC151 Homework 14.
Warning: So that this exercise is a learning assignment for everyone, I may spend class time publicly critiquing your work.
As you've seen in our discussions this past week, one of the
key ideas in functional programming is that you can factor out
common control structures. We've seen it possible to factor out the
process of building a new list by recursing over the list with map
and to factor out the process of checking all the values in a list with
listof?
.
Here's another common task: Counting values that match some predicate. We've written procedures that count the number of symbols in a list and that count the number of odd numbers in a list of numbers.
(define tallysymbols (lambda (lst) (cond ((null? lst) 0) ((symbol? (car lst)) (+ 1 (tallysymbols (cdr lst)))) (else (tallysymbols (cdr lst)))))) (define tallyodds (lambda (lst) (cond ((null? lst) 0) ((odd? (car lst)) (+ 1 (tallyodds (cdr lst)))) (else (tallyodds (cdr lst))))))
a. Write a procedure, (tally pred? lst)
, that counts the
number of values in lst
for which pred?
holds.
b. Rewrite tallysymbols
and tallyodds
using tally
. That is, your definitions should look
something like the following:
(define tallysymbols (lambda (lst) (tally ...)))
c. Write a procedure, tallyas
, which takes a list of
integers (representing grades) as a parameter and returns the number of
values that are 90 and above. You should not verify the preconditions
of the procedure (that is, do not check that it's a list of integers).
d. Write a procedure bettertallyodds
, which takes a list
of arbitrary Scheme values as a parameter and returns a count of the
number of times an odd integer appears in the list.
> (tallyodds (list 'a 'b 3)) odd?: expects argument of type *lt;integer>; given a > (bettertallyodds (list 'a 'b 3 "hello" list 1)) 2
You should write bettertallyodds
so that its body
is a call to tally
with an appropriate predicate as the
parameter.
e. [Optional] Write tallyodds
without using a
lambda. Hint: You might want to use leftsection
,
rightsection
, or compose
.
Thursday, 2 November 2006 [Samuel A. Rebelsky]
Tuesday, 17 April 2007 [Samuel A. Rebelsky]
tallyodds
and the related
procedures.
bettertallyodds
) and
e (definitions without lambdas).
[Skip to Body]
Primary:
[Front Door]
[Glance]

[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Assignment]
Groupings:
[Assignments]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151 20056F (Rebelsky)]
[CSC151.01 2007S (Davis)]
[CSCS151 2005S (Stone)]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Wed Aug 29 08:59:27 2007.
The source to the document was last modified on Sun Jul 29 16:12:59 2007.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2007F/Homework/hw.14.html
.
You may wish to validate this document's HTML ; ;
Samuel A. Rebelsky, rebelsky@grinnell.eduhttp://creativecommons.org/licenses/bync/2.5/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.