Fundamentals of CS I (CS151 2002F)

Exam 1

Distributed: Wednesday, October 2, 2002
Due: Friday, October 11, 2002
No extensions.

This page may be found online at


Useful Files


There are nine problems on the exam. Each problem is worth the eleven 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 your 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 eight 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 unaware 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.

Answer all of your questions electronically and turn them in in hardcopy. That is, you must write all of your answers on the computer and print them out. You should also email me a copy of your exam by copying your exam and pasting it into an emal message. Put your answers in the same order that the problems appear in. 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, 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 produces, 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 also 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.

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.


Part A: Swapping and assoc

Key Topics: Recursion, Association Lists, Documentation, Lists

Sarah and Steven Schemer read my description of look-up-by-number from the reading on association lists and noted that it seemed surprisingly repetitious of the code for assoc. Their reaction? Instead of rewriting assoc, they suggest writing a procedure that updates the directory so that the phone number appears first.

For example, if you run that procedure on the list

(("Samuel A. Rebelsky" "4410" "Technology Studies")
 ("Russell K. Osgood"  "3000" "The College"))

You should end up with the list

(("4410" "Samuel A. Rebelsky" "Technology Studies")
 ("3000" "Russell K. Osgood"  "The College))

Once you've written such a procedure (say, swap-name-and-phone), it's trivial to write look-up-by-number.

(define look-up-by-number
  (lambda (number directory)
    (assoc number (swap-name-and-phone directory))))

Problem 1: Document swap-name-and-phone

Document swap-name-and-phone using the Six-P documentation style. Think carefully about how you describe the initial directory and the result directory.

Problem 2: Implementation and Testing

Write and test swap-name-and-phone.

Problem 3: Improving Results

The result of the new look-up-by-number given above presents the number before the name. Fix look-up-by-number so that it presents its result in the name/number/dept form.

Part B: From Structures to Strings

Key Topics: Recursion, Pairs and Lists, Strings, Predicates, Scheme Types

Here's a procedure that converts values built from cons cells, nulls, and numbers to strings similar to those Scheme would use.

;;; Procedure:
;;;   value->string
;;; Parameters:
;;;   val, a stringable value
;;; Purpose:
;;;   Converts val to a string that, when displayed, is the same
;;;   as val.  (Exists mainly as a way of thinking about how Scheme
;;;   shows lists.)
;;; Produces:
;;;   str, a string
;;; Preconditions:
;;;   val must be stringable.  A value is stringable if it is either
;;;   (1) null, (2) a number, (3) a pair of two stringable things.
;;; Postconditions:
;;;   str is a string that represents val.  In particular, if str
;;;   is displayed, it should be equal to value (not necessarily 
;;;   precisely the same form, but an equal value).
(define value->string
  (lambda (val)
      ((null? val) "()")
      ((number? val) (number->string val))
      ((pair? val) 
         "(" (value->string (car val))
             (vts-helper (cdr val)) ")" ))
      (else "???"))))

(define vts-helper
  (lambda (val)
      ((null? val) "")
      ((pair? val) 
       (string-append " " (value->string (car val)) 
                      (vts-helper (cdr val))))
      (else (string-append " . " (value->string val))))))

Here are some sample interactions with value->string

> (value->string (list 1 2 3))
"(1 2 3)"
> (value->string (list (list 1 2) 3))
"((1 2) 3)"
> (value->string (list null null null))
"(() () ())"
> (value->string (cons 1 2))
"(1 . 2)"

Problem 4: A Predicate for Preconditions

In order for value->string to work correctly, the parameter must be stringable. Hence, it is useful to have a procedure that determines whether or not a value is stringable.

Document, write, and test (stringable? val), a procedure that determines whether a value is stringable according to the definition above.

Problem 5: Adding Symbols

Update value->string so that it also supports symbols.

For example,

> (value->string 'a)
> (display (value->string 'a))
> (value->string (list 'a 'b)
"(a b)"

Note that you may find it helpful to use symbol->string.

Problem 6: Adding Strings

Update value->string so that it also supports strings.

For example,

> (value->string "a")
> (display (value->string "a"))
> (value->string (list "ab"))
> (value->string (list "ab" "a" "b"))
"(\"ab\" \"a\" \"b\")"
> (display (value->string (list "ab" "a" "b")))
("ab" "a" "b")

Note that we need the extra quotation marks to distinguish the string "a" from the symbol a.

Section C: Evaluating Email

Key Topics: Recursion, Numbers, Reading Code

Sarah and Steven Schemer receive too much email (don't we all) and they've decided to write a program to help evaluate their email before they read. They've heard about a neat statistical technique for evaluating the potential interestingness of email:

In an all-night session in the MathLan, they've written most of their program, which you can find as They were able to figure out how to extract all the words from an email message and to build a histogram of words.

Unfortunately, they ran out of energy and brain cells when it came time to do the statistical analysis. They've turned to you for help.

As you can see, the rely on extract-words to build the list of words and make-histogram to turn that list into a histogram. A histogram looks something like the following

(("rebelsky" 5)
 ("cs151" 2)
 ("grade" 4)
 ("grinnell" 1)
 ("darby" 2)
 ("politics" 8)
 ("recycling" 1)
 ("zebras" 2))

The precedence list used to evalaute email looks suspiciously similar, except that the second value is the value of the word. Here's Sarah's initial list:

(define sarahs-metrics
  '(("sarah" 10)      ; Mail with my name is interesting
    ("scheme" 10)     ; Scheme is my favorite language
    ("blue" 2)        ; I like the color and sometimes friends use it
                      ; to indicate that they are sad
    ("grinnell" 5)    ; Well ...
    ("planning" 20)   ; The hot topic of the moment
    ("CS151" 6)       ; Not my fave course, but ...
    ("grade" 8)       ; Yeah, I worry about grades
    ("rebelsky" 1)    ; A good professor, but ...
    ("cartridge" -5)  ; Sounds like spam
    ("cartridges" -5) ; Sounds like spam
    ("hotmail" -6)    ; Sounds like spam

Problem 7: Lookup Up the Values of Words

As you may have noted, the implementation of word-value is currently incorrect, as it returns 0. Update word-value so that it looks up the word in word-values and returns the associated value. If the word does not appear in word-values, use the value 0.

Problem 8: Computing the Values of Histograms

You may also have also noted that email-value-helper only looks at the first word/occurences entries in the histogram. Update it so that it uses all the entries.

For example, if the histogram looks like

(("rebelsky" 5)
 ("planning" 2)
 ("cartridge" 3))

Using Sarah's metric, email-value-helper should return 30 (that is, 5*1 + 2*20 + 3*-5).

Problem 9: Experimental Evaluation

Create your own metric, using at least fifteen words. Pick a few recent relatively short email messages that you're willing to let me see, and compute their value using email-value. Make sure to include your results in the exam you turn in.

What do you think about the effectiveness of this procedure?



Tuesday, 1 October 2002 [Samuel A. Rebelsky]

Wednesday, 2 October 2002 [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 Dec 3 22:38:26 2002.
The source to the document was last modified on Wed Oct 2 08:43:11 2002.
This document may be found at

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

Samuel A. Rebelsky,