CSC 160 HW9
Assigned due 1 May, due 10 May
For all the programming assignments, be sure to follow the design recipe.
Write
your function contract, examples, and function definition in the Definitions
Window, save it to a file, and send me this file. Also, test
your program by typing each of your examples into the Interactions Window,
save it to a file, and send me this file. Be sure to choose meaningful
names for functions and parameters, and watch for opportunities to use
functions you've already written.
Also turn in a log of how many errors of different kinds you
encountered in the assignment, with brief comments describing each one
("mismatched parentheses" is self-explanatory, but more complex errors
might need more description).
This assignment is to be done in pairs, as before.
- Define a struct to represent a person, with a name and
an age. Develop a function oldest, which takes a
non-empty list of persons, and returns the oldest person.
- Do exercises
12.2.1
and
12.2.2.
(Notice the sentence in the textbook, "The function must take
advantage of the fact that the list is sorted"! The function provided in the
textbook is already correct; yours should be correct but more efficient.)
- Do exercise
12.3.1.
- Do (but don't turn in) exercise
12.4.1.
Do exercise
12.4.2,
which depends on problem 12.4.1 above.
(The book develops arrangements,
using the auxiliary function insert-everywhere/in-all-words,
which you are to write. It'll probably require two more auxiliary
functions of its own. Be careful with types: a list of symbols is not
the same as a list of lists of symbols!)
- Do (but don't turn in) some of exercises
18.2.1,
18.2.2,
18.2.3,
18.2.4,
18.2.5,
and
18.2.6.
(Note: The printed book and the on-line book
disagree on the problem numbers; the on-line book calls these 18.1.1,
18.1.2, etc.)
- Do exercise
18.2.15.
(We did exercise
18.2.12
in class.)
- Do exercises
19.1.5
and
19.1.6.
- Develop a cooperating pair of functions named count-calls
and reset. Each function takes no arguments (which means you need to
be in Advanced Student mode to write them).
reset returns nothing at all, while
count-calls returns how many times it has been called since
the last time reset was called. (Such a pair of functions might be
used, for example, in a hit counter for a Web page.)
Hint: Use a global variable, which both functions can
modify, to keep track of the count.
- Develop a cooperating pair of functions named init
and next. init takes in a list, and returns nothing.
next takes in nothing, and returns the "next" element of the
list that was given to init. For example,
(init (list 'a 'b 'c)))
(next)
'a
(next)
'b
(next)
'c
What do you think next should do if it is called a fourth time?
Extra credit:
Develop a single function named make-iterator which
takes in a list and returns a function (like next, above) which
steps through that list. This approach is more flexible, in that it
allows you to have several different lists going at once, stepping
through each one independently. For example,
(define next-name (make-iterator (list 'joe 'mary)))
(define next-class (make-iterator (list 'cs160 'math140 'eng107)))
(next-name)
'joe
(next-class)
'cs160
(next-class)
'math140
(next-name)
'mary
(next-class
'eng107
Hint: You'll need at least one local variable,
probably two, and perhaps a locally-defined function as well. It took me
7-10 lines of code, depending on what language features I used and whether
I did error-checking.
Last modified:
Stephen Bloch / sbloch@adelphi.edu