Computer Science Fundamentals (CS153 2004S)

Exam 2: Algorithms and Higher-Order Procedures

Distributed: Wednesday, 3 March 2004
Due: 10:00 a.m., Wednesday, 10 March 2004
No extensions.

This page may be found online at



There are four problems on the exam. Some problems have subproblems. Each full problem is worth twenty-five points. The point value associated with a problem does not necessarily correspond to the complexity of the problem or the time required to solve the problem.

This examination is open book, open notes, open mind, open computer, open Web. However, it is closed person. That means you should not talk to other people about the exam. Other than that limitation, you should feel free to use all reasonable resources available to you. As always, you are expected to turn in your own work. If you find ideas in a book or on the Web, be sure to cite them appropriately.

Although you may use the Web for this exam, you may not post your answers to this examination on the Web (at least not until after I return exams to you). And, in case it's not clear, you may not ask others (in person, via email, or by posting a please help message) to put answers on the Web.

This is a take-home examination. You may use any time or times you deem appropriate to complete the exam, provided you return it to me by the due date.

This exam is likely to take you about four to six hours, depending on how well you've learned topics and how fast you work. You should not work more than eight hours on this exam. Stop at eight hours and write There's more to life than CS and you will earn at least 80 points on this exam. I would appreciate it if you would write down the amount of time each problem takes. I expect that someone who has mastered the material and works at a moderate rate should have little trouble completing the exam in a reasonable amount of time. Since I worry about the amount of time my exams take, I will give two points of extra credit to the first two people who honestly report that they've spent at least five hours on the exam or completed the exam. (At that point, I may then change the exam.)

You must include both of the following statements on the cover sheet of the examination. Please sign and date each statement. Note that the statements must be true; if you are unable to sign either statement, please talk to me at your earliest convenience. You need not reveal the particulars of the dishonesty, simply that it happened. Note also that inappropriate assistance is assistance from (or to) anyone other than myself or our teaching assistant.

1. I have neither received nor given inappropriate assistance on this examination.
2. I am not aware of any other students who have given or received inappropriate assistance on this examination.

Because different students may be taking the exam at different times, you are not permitted to discuss the exam with anyone until after I have returned it. If you must say something about the exam, you are allowed to say This is among the hardest exams I have ever taken. If you don't start it early, you will have no chance of finishing the exam. You may also summarize these policies. You may not tell other students which problems you've finished. You may not tell other students how long you've spent on the exam.

You must both answer all of your questions electronically and turn in a printed version of your exam. That is, you must write all of your answers on the computer, print them out, number the pages, put your name on every page, and hand me the printed copy. You must also email me a copy of your exam by copying your exam and pasting it into an email message. Put your answers in the same order as the problems. If you write your name at the top of each sheet of the printed copy, you will earn two points of extra credit.

In many problems, I ask you to write code. Unless I specify otherwise in a problem, you should write working code and include examples that show that you've tested the code.

You should document all of your primary procedures. In most cases, a few sentences will suffice. In a few cases, I'll ask you to provide the full documentation (including parameters, purpose, value produced, preconditions, and postconditions). If you write helper procedures (and you may certainly write helper procedures) you should document those with a few short notes. When appropriate, you should include short comments within your code. You should also take care to format your code carefully.

Just as you should be careful and precise when you write code and documentation, so should you be careful and precise when you write prose. Please check your spelling and grammar. Since I should be equally careful, the whole class will receive one point of extra credit for each error in spelling or grammar you identify on this exam. I will limit that form of extra credit.

I will give partial credit for partially correct answers. You ensure the best possible grade for yourself by emphasizing your answer and including a clear set of work that you used to derive the answer.

I may not be available at the time you take the exam. If you feel that a question is badly worded or impossible to answer, note the problem you have observed and attempt to reword the question in such a way that it is answerable. If it's a reasonable hour (before 10 p.m. and after 8 a.m.), feel free to try to call me in the office (269-4410) or at home (236-7445).

I will also reserve time at the start of classes this week and next to discuss any general questions you have on the exam.


Problem 1: Cloning Values

Topics: Recursion, Lists, Vectors, Memory

As you may have noted, in a recent class, Saul suddenly suggested that it would be useful to have a way of making copies of vectors and lists. Erik enthusiastically echoed this request.

a. Write a procedure, (clone-list lst), that creates a copy of lst that uses different memory cells.

b. Write a procedure, (clone-vector vec), that creates a copy of vec that uses different memory cells.

c. Write a predicate, (clones? left right) that determines whether or not left and right are clones of each other. Two vectors are clones if they have the same length and contain the same values in corresponding locations, but do not share memory locations. Two lists are clones if (1) their cars are equal, (2) their first pairs are in different memory locations, and (3) their cdrs are clones. No two other values are clones.

You need not document any of these procedures.

Problem 2: A Generalized less-than? Procedure

Topics: Algorithms, Strings, Comparison, Deep Recursion, Vectors

Cole and Coco Coder are concerned that they have to keep rewriting similar less-than? predicates for binary search and for sorting algorithms. For example, they must remember to use

They've decided that someone should write a general less-than? predicate that can compare any reasonable pairs of values. Of course, Cole and Coco are much better at deciding what they'd like to do than at actually doing it. Hence, they've asked you to implement less-than? for them. Your sentient and malicious instructor agrees.

Here's what Cole and Coco would like you to do for less-than?.

a. Implement the less-than? procedure. You need not write the full six P's for this procedure, but you should summarize what it does.

b. Theodore and Thelma Theoretician take one look at Cole and Coco's suggestions and start to chuckle. They say

One of the key characteristics of the standard may-precede? parameter to sorting and searching algorithms is that it is transitive. That is, if a may precede b and b may precede c, then a may precede c. Unfortunately, there are values for which less-than? is not transitive.

Find three basic values, a, b, and c, such that

> (less-than? a b)
> (less-than? b c)
> (less-than? a c)

By basic, I mean that a, b, and c should not be lists or vectors.

Problem 3: Generalized section

Topics: Higher-order procedures, Sectioning, Variable-Arity procedures

Hiram and Olivia Higher-Order love right-section and left-section because these two procedures not only take procedures as parameters, but also return new procedures as results. However, they are concerned that these two procedures are insufficiently general. After all, they reason, given that these two procedures do almost exactly the same thing (that is, fill in one parameter of a two-parameter procedure), why do we need separate procedures?

They suggest that we instead create a multi-arity procedure, section, that takes as parameters (1) an n-ary procedure, proc and (2) n additional parameters, param1 ... paramn, each of which may either be set values or the special symbol '?. Given k of the special '? symbols, it creates a new k-ary procedure, sectioned. The sectioned procedure calls proc with either parami, if parami is not the special symbol, or the appropriate parameter to sectioned, otherwise.

With the new section procedure, we can write (section proc '? val) instead of (right-section proc val) and (section proc val '?) instead of (left-section proc val).

For example, we can write a procedure that divides its parameter in half with

(define half (section / '? 2))

Similarly, with the new section, one can simply write valid-grade? (remember that procedure from early in the semester?) with

(define valid-grade? (section <= 0 '? 100))

Similarly, we can create a procedure that rolls a 0-5 die with

(define die (section random 6))

The '? symbol can also appear multiple times. For example, you might write a procedure that asks whether two numbers have opposite sign with

(define opposite-sign? (section < '? 0 '?))

Yes, I realize that's not a very good example and that the negative number must come first, but hey, it's early in the morning.

a. Write section. You need not document it.

b. Show five interesting uses of section to simplify the definition of other procedures we've written this semester.

Problem 4: Documenting Procedures

Topics: Documentation, Recursive procedures

An and Al Abbrev object to the overly-long procedure names that Sam likes to use. Hence, they tend to choose one-character names. They also avoid the sixP Ps that I like. Here's a procedure they've recently written.

(define r
  (lambda (l p?)
    (letrec ((c (lambda (p v)
                  (let ((x (if (p? v) 1 0)))
                    (cons (+ (car p) x) (+ (cdr p) (- 1 x))))))
             (r (lambda (q m)
                  (if (null? m) 
                      (/ (car q) (cdr q))
                      (r (c q (car m)) (cdr m))))))
      (r (cons 0 0) l))))

Document the procedure r. If you'd like, you can change the names used within r (including r) to something more readable.

Some Questions and Answers

These are some of the questions students have asked about the exam and my answers to those questions.

Should we use eq? to compare memory locations?
What should we do for less-than? with pairs?
If the first element of the first pair is smaller, return true (#t). If the first element of the first pair is larger, return false (#f). Otherwise, recurse on the second element.
Do we have to handle complex numbers in less-than??
Do you hate us?
No. I find you a nice group of students to teach.
That's not a question.
On problem 2, we should be able to compare vectors to lists and lists to vectors using the element-by-element strategy, right?
Do we get extra credit for identifying errors in the question and answer section?
Traditionally, yes. However, you have now reached the limit of extra credit.
Do we get extra credit for identifying errors in the errors section?
See the previous answer.
What's the difference between string-ci<? and string<??
The former is case insensitive, the latter is case sensistive.
When should we convert values to strings in Problem 2?
Clearly, when you're dealing with characters, complex numbers, and symbols. In addition, when you're comparing numbers to non-numeric basic types, you'll need to convert those numbers to strings.
Can you give me some hints on section?
Since you can't determine the arity of the result procedure in advance, make it variable arity and the check the number of parameters.


Here you will find errors of spelling, grammar, and design that students have noted. Remember, each error found corresponds to a point of extra credit for everyone. I limit such extra credit to five points.



Tuesday, 2 March 2004 [Samuel A. Rebelsky]

Wednesday, 3 March 2004 [Samuel A. Rebelsky]

Thursday, 4 March 2004 [Samuel A. Rebelsky]

Friday, 5 March 2004 [Samuel A. Rebelsky]


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 Fri May 7 09:43:53 2004.
The source to the document was last modified on Sat Mar 6 17:09:54 2004.
This document may be found at

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky,