Programming Languages (CS302 2005S)

Past-midsemester Examination

Distributed: 9:00 a.m., Monday, 18 April 2005
Due: 9:00 a.m., Friday, 29 April 2005
No extensions.

This page may be found online at



There are six (6) problems on the exam. Some problems have subproblems. Different problems have different point values. 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. (For the purposes of this rule, Sam Rebelsky is not a person.) 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, via IM, by posting a please help message, or in any other way) 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 ten to twelve hours, depending on how well you've learned topics and how fast you work. You should not work more than fourteen hours on this exam. Stop at fourteen hours and write There's more to life than CS and you will earn at least 70 points on this exam. I would appreciate it if you would write down the amount of time each problem takes. Each person who does so will earn two points of extra credit. 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.

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 Professor Rebelsky (that's me).

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. Put your answers in the same order as the problems. Please write your name at the top of each sheet of the printed copy. Doing so will earn you two points of extra credit.

Your electronic exam may be written in plain text, LaTeX, plain TeX, HTML, XHTML, or even Microsoft Word. (Yes, other formats are reasonable, too, but check with me first.)

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.

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 Monday) to discuss any general questions you have on the exam.


Problem 1: Comparative Functional Languages [15 points]

Fiona and Fred Functional know that they should use functional languages for all of their programming. However, they often have trouble deciding which functional language is best suited to a particular task.

Write them a short (one page or less) guide that they can consult each time they encounter a new programming problem and that will help them select between Haskell, FP, and Scheme. Note that you are not permitted to simply say Use Scheme (or Use ML or Use Haskell or Use FP or whatever). Fiona and Fred know that each language has certain advantages and disadvantages that make each better or worse for certain applications.

I will grade your solution on both what it reveals about your knowledge of the language and on what you suggest as criterion for selecting a language. Note also that your answer should not speak only to the features of the language, but rather to why those are important features. For example, If you need laziness, use Haskell is not acceptable, nor is If you need to design your own syntax, use Scheme because it has Macros.

Problem 2: Functional C (or Java) [10 points]

Harry and Hannah Hacker go to a large institution and act confused whenever they hear that we teach functional programming at Grinnell. After all, they note, C and Java are functional, too. You can write functions, you can pass functions as parameters. C and Java are just more efficient.

Any competent C programmer knows how to pass functions, so I won't describe how here. In Java, you can wrap a function in a class that implements the Function interface, and that interface provides one method, apply(Object[] params). For example, here's how we might write map.

public static Object[] map(Function f, Object[] stuff)
  Object[] results = new Object[stuff.length];
  for (int i = 0; i < stuff.length; i++) {
    results[i] = f.apply(new Object[] { stuff[i] });
  } // for
} // map(Function, Object[])

Unfortunately, because Harry and Hannah are at a large institution, they've only had to take CS courses and have not learned how to write a persuasive argument. It is therefore up to you. Of course, as a good Grinnellian, you not only know how to write persuasively, you know how to write persuasively about both sides of an issue.

a. In a paragraph or two, argue for their claim as it applies to either C or Java (your choice).

b. In a paragraph or two, argue against their claim as it applies to either C or Java (your choice, but it must be the same choice as in part a).

I will grade your solution on what it shows you understand and do not understand about what might be the essences of functional programming. I will also grade your solution on how compelling your arguments are.

Problem 3: Programming Paradigms [20 Points]

Presha and Pringle Prospie are confused by Grinnell's introductory curriculum. They ask, Why don't you just teach everything in C++, like they do at ______?

In a few coherent sentences each, describe the three problem-solving paradigms we teach in Computer Science at Grinnell (functional, imperative, and object-oriented). Then, in a short paragraph, explain why it is useful to learn all three paradigms early in your career.

I will grade your solution on what it shows you understand and do not understand about these three paradigms. I will also look for a convincing argument for our introductory sequence.

Problem 4: Currying [10 points]

Steven and Sarah Schemer are confused by the notion of Currying. If it was useful, they note, McCarthy would have put it in LISP and that Steele and the many reporters would have put it into Scheme. They are not impressed by my retort that Currying is in Scheme, it's just in the definition of Scheme.

a. Explain what Currying is.

b. Explain why it's useful.

Problem 5: Gries's Notation [15 points]

Now that we're teaching Gries early in the Computer Science curriculum, the students learning the science of programming are clamoring for more supporting materials. Unfortunately, one member of the department is too likely to write a thousand-page supporting guide completely in formal notation; one member of the department wrote a guide two years ago, but lost it in his or her office; one member of the department thinks students are better served by one-on-one discussions than by handouts; one member of the department has the best intentions, but never seems to get anything written; and one member of the department thinks we don't want to keep or encourage students who can't figure it out from the book and class. (Yes, that's more faculty than we have. Perhaps more than one description applies to the same faculty member. Perhaps they all apply to me.)

Hence, the task falls to you. The first thing we need is a summary of all the notation Gries uses in Chapters 0 though 10. Write that summary.

Problem 6: The Semantics of an Imperative Language [30 points]

Ev and Evan Evil are unhappy about the previous problems on this exam. Sam, you wuss, they begin, almost all of your questions are essay-style and permit multiple correct answers. Why don't you have a problem that requires careful mathematics? Unfortunately for you, it's pretty clear to me that Ev and Evan will soon be trustees, and it therefore does me good to follow their requests. So ... here goes.

Using a denotational semantic style similar to that from the Scheme report (and certainly including both a store and an environment), write a semantics for the following language, which we'll call E.

Programs in E are sequences of statements separated by semicolons. (There are no functions or function calls.) There are six kinds of statements:

Each block in E introduces a new scope, which affects the use of variables. A variable may be used in a non-declaration statement only if it is previously declared in the same scope or an enclosing scope. A variable declaration in a scope hides any variable with the same name in any enclosing scope. (That is, changes to the variable do not affect the variable with the same name in the enclosing scope.) Variables may be declared more than once in the same scope, with whatever effect you deem appropriate. (If you'd prefer not to allow variables to be redeclared in the same scope, that's okay, too, but you do need to allow them to be declared in a new scope.)

Variables in E only take on integer values.

If the variable in the conditional is non-zero, the consequent is executed. If the variable in the conditional is zero, the alternate is executed. If the variable in the conditional is undefined, crash and burn as you deem appropriate.

Expressions in E are either a unary prefix operator or a binary infix operator applied to two operands. The operands can be variables or integer constants, or both. Valid unary operators include abs, square, and - (negate). Valid binary operators include + (add), - (subtract), * (multiply), / (integer division), mod (modulus), and ^ (exponentiate).

Some Questions and Answers

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


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.



Sunday, 17 April 2005 [Samuel A. Rebelsky]

Monday, 18 April 2005 [Samuel A. Rebelsky]

Thursday, 28 April 2005 [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 Thu Apr 28 13:58:10 2005.
The source to the document was last modified on Thu Apr 28 12:57:44 2005.
This document may be found at

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

Samuel A. Rebelsky,