Computer Science Fundamentals (CS153 2003S)

Exam 1: Recursion

Distributed: Wednesday, 12 February 2003
Due: 10 a.m., Friday, 21 February 2003
No extensions.

This page may be found online at


Useful Files


There are five problems on the exam. Some problems have subproblems. Each full problem is worth twenty 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. If you write down the amount of time you spend on each problem, I'll give you a half point of extra credit per 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. It is likely to take you about five to ten hours, depending on how well you've learned topics and how fast you work. 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 three points of extra credit to the first two people who honestly report that they've spent at least seven hours on 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. 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 them in in hardcopy. That is, you must write all of your answers on the computer, print them out, 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. Make sure that your solution confirms to the format for laboratory writeups

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 fully document all of the primary procedures (including parameters, purpose, value produced, preconditions, and postconditions). If you write helper procedures (and you may certainly write helper procedures) you should document those, too, although you may opt to write less documentation. 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, 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 to five points.

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: Sum of Digits

Key Topics: Numeric Recursion

Document, write, and test a Scheme procedure, (sum-of-digits val), that sums all the digits in the non-negative integer value val.

Problem 2: HTML Markup

Key Topics: List Recursion, Strings

Document, write, and test a Scheme procedure, (markup tag attribute-list contents-1 ... contents-n), that generates a string for HTML, using tag as the outermost tag, with attribute-list as the attributes, and contents-1 through contents-n as contents. Each contents-i is a string. attribute-list is a list of lists, with each component list of either length two (attribute and value) or length one (attribute with no value).

For example,

> (markup "p" '(("class" "question")) "Hello." "Goodbye")
"<p class=\"question\">Hello.Goodbye</p>
> (display (markup "form" 
                   '(("method" "get") ("action" "file.cgi"))
                   (string #\newline)
                   "<input type=\"text\" name=\"whatever\">"
                   (string #\newline)))

<form method="get" action="file.cgi">
<input type="text" name="whatever">

Problem 3: Determining Membership (1)

Key Topics: List Recursion, Equality Testing, Predicates

Document, write, and test a procedure, (member? value list-of-values) that determines if value appears anywhere within list-of-values. Use equal? to compare value to each value in list-of-values in sequence.

Problem 4: Determining Membership (2)

Key Topics: List Recursion, Deep Recursion, Equality Testing, Predicates

Write and test a procedure, (contains? nested-list-of-values value), that determines whether value appears in nested-list-of-values, or a sublist of nested-list-of-values, or a sub-sublist, and so on and so forth.

You need not document this procedure.

Problem 5: Set Operations

Key Topics: List Recursion, Data Structures

Assume that we have chosen to represent sets of values as lists of values with no duplicates. For example, the set { 1 2 3 } might be represented by the list (1 2 3) or the list (3 1 2) or the list (2 1 3) or so on and so forth.

a. Write a procedure, (intersect set1 set2), that produces the intersection of two sets. The intersection of two sets consists of only those values that appear in both set1 and set2.

b. Write a procedure, (union set1 set2), that produces the union of two sets. The union of two sets consists of only those values that appear in set1 or set2.

Note that this or is intended to be inclusive.

c. Write a procedure, (difference set1 set2), that produces a set that consists of the values that appear in set1 that do not appear in set2.

d. Write a predicate, (subset? set1 set2), that holds whenever all the elements of set1 appear within set2.

You need not document or show testing for these procedures.

In your answers to these questions, you may certainly use the member? procedure that you implemented in the previous problem or the built-in member procedure, which essentially accomplishes the same task.

Some Questions and Answers

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

Can you explain what you mean by either set1 or set2 in the informal definition of union?
I mean that the value can appear in set1 or set2 or both. I've tried to make the instructions more readable.
Do we have to do the full six-P documentation for internal kernels?
No, but a short sentence or two wouldn't hurt.
What type should subset return?
For intersection, union, and difference, if both lists contain a value twice, should the list returned contain one instance of that value or two?
A precondition of all three procedures is that sets are represented as lists with no duplicate values. If a caller fails to meet the preconditions, you can do whatever you want.
I hate typing long names like nested-list-of-values Can't I just call it l?
Experience shows that short variable names lead to less readable code, but you are free to do what you want.
What should sum-of-digits do for negative numbers?
A precondition of sum-of-digits is that val is non-negative. (Okay, it wasn't an explicit precondition in the initial formulation, but it should have been.)




Tuesday, 11 February 2003 [Samuel A. Rebelsky]

Wednesday, 12 February 2003 [Samuel A. Rebelsky]

Thursday, 13 February 2003 [Samuel A. Rebelsky]

Monday, 17 February 2003 [Samuel A. Rebelsky]

Tuesday, 18 February 2003 [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 Tue May 6 09:19:03 2003.
The source to the document was last modified on Tue Feb 18 07:57:32 2003.
This document may be found at

Valid HTML 4.0 ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky,