Fundamentals of Computer Science 1 (CS151 2003S)

Exam 2: Recursion

Distributed: Monday, 31 March 2003
Due: noon, Tuesday, 8 April 2003
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. It is likely to take you about three to six 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 five 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 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. Make sure that your solution confirms to the format for laboratory writeups (except that you should not tell me the location in the MathLAN or on the Web, since your code should not be published).

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 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'll be gone from noon Friday to 5 p.m. Sunday, so don't call during those times.]

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


Problem 1: Counting Digits

Key Topics: Numeric Recursion

Document, write, and test a Scheme procedure, (count-appearances digit val), that counts how many times the one-digit integer digit appears in the non-negative integer value val.

For example,

> (count-appearances 1 12)
> (count-appearances 1 312)
> (count-appearances 1 10312101)
> (count-appearances 0 10312101)
> (count-appearances 5 10312101)
> (count-appearances 0 000100)

Problem 2: Determining Membership

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

Write and test a procedure, (in-tree? value tree-of-values), which determines whether the simple value value is either equal to tree-of-values or appears somewhere in tree-of-values, where tree-of-values is built by combining a number of cons cells (either explicitly, with cons or implicitly, with list).

You need not document this procedure.

> (in-tree? 0 null)
> (in-tree? 0 0)
> (in-tree? 0 1)
> (in-tree? 0 (cons 1 1))
> (in-tree? 0 (cons 0 1))
> (in-tree? 0 (cons 1 0))
> (in-tree? 0 (cons 1 (cons 0 2)))
> (in-tree? 0 (list 0))
> (in-tree? 0 (list 5 4 3 2 1 0))
> (in-tree? 0 (list (list "one" 2 'three) (list 0) (list 2)))

Problem 3: Converting Lists to Strings

Steven and Sarah Stringer find it fascinating that Scheme can figure out how to print such a wide variety of Scheme types. They find it particularly interesting that Scheme is able to print out lists without knowing their length in advance. They've asked you to show them some code that explains what Scheme does.

Write a procedure, (list-of-integers->string lst), which takes a list of integers and converts it to the corresponding string.

For example,

> (list-of-integers->string null)
> (list-of-integers->string (cons 1 null))
> (list-of-integers->string (list 1 2))
"(1 2)"
> (list-of-integers->string (cons 1 (cons 2 (list 5 4 3))))
"(1 2 5 4 3)"

You will probably find it useful to use the built-in number->string procedure. I expect that you will also want to create a helper procedure to deal with all but the first element of the list.

If you are feeling particularly ambitious (that is, this part of the problem is optional), you can handle nested lists and things built of cons cells that aren't necessarily lists.

Problem 4: Documenting Procedures

An and Al Abbrev object to the overly-long procedure names that Sam likes to use, like list-of-integers->string. Hence, they tend to choose one-character names. They also avoid the six P's 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))))

a. Change the various names in the procedure to clarify what the procedure does.

b. Add internal comments to explain the various parts.

c. Add introductory comments (the six P's) to explain the purpose (and the other P's) of the procedure.

Some Questions and Answers

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

In problem 2, can we restrict value to real numbers or do we have to deal with other kinds of values?
You have to deal with all simple values. See the previous exam for more details about simple values.
In problem 3, can we skip the parens?
Certainly not. However, you may want to write a helper that generates the string without the parens.
In problem 2, what is a tree?
A tree is either (1) a non-pair value or (2) a pair of two trees.
Would you accept an answer that adds an extra space at the end of a list for problem 3? E.g., "(1 2 3 )" instead of "(1 2 3)"?
That would not be a perfectly correct answer. Expect to lose about five points for an otherwise-correct answer that includes the extra space.


Here you will find the errors of spelling, grammar, and external design. Remember, each error found gets the whole class a point of extra credit on the exam (with a maximum of five such points).



Sunday, 30 March 2003 [Samuel A. Rebelsky]

Monday, 31 March 2003 [Samuel A. Rebelsky]

Tuesday, 1 April 2003 [Samuel A. Rebelsky]

Friday, 4 April 2003 [Samuel A. Rebelsky]

Sunday, 5 April 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:22:18 2003.
The source to the document was last modified on Sun Apr 27 19:00:41 2003.
This document may be found at

Valid HTML 4.0 ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky,