Programming Languages (CSC-302 99S)

Exam 2: Semantics, Functional Programming, and More

This page may be found online at

Distributed: Friday, March 12, 1999
Due: Start of class, Friday, March 19, 1999
No extensions!


There are four questions on this exam. Each has equal point value, although each may take a different amount of time for you to complete.

If you are confused by any question, do not hesitate to ask me for clarification. I will reserve time at the start of classes next week for questions. In addition, you can speak to me individually. I may not be available at the time you are working on the exam. 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). Email is also a reasonable way to contact me.

This examination is open book, open notes, open mind, open computer, open Web. Feel free to use any and all resources available to you except for other people and answer keys. That is, you should not consult other students, other faculty, or anyone else. (If someone outside of Grinnell has already created a web page that answers your question, that's okay; however, you can't ask someone to create such a page.) In addition, while you can look at the pages I've created for this and other courses, you should not refer any pages that are clearly answer keys to homework assignments of exams.

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.

This is a take-home examination. It is likely to take you about four hours. You may use any time or times you deem appropriate to complete the exam, provided you return it to me by the due date. No late exams will be accepted. I will make a solution key soon after the exam.

Because different students will 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 definitely the hardest exam I have ever taken.'' You may also summarize these policies.

Answer all of your questions electronically. That is, you must write all of your answers on the computer and print them out. Please put your answers in the same order that the problems appear in.

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


1. Input Expressions in SIMPLE

In some languages, it's possible to use the input statement in expressions, so that it reads and returns a value, rather than reading a value and storing it in a variable. For example, instead of


you would write

x = read();

You can also use read as part of an expression, as in

x = 2 * read() - 5;

Consider the accompanying description of the semantics of SIMPLE. What changes would we need to make to the SIMPLE semantics to accommodate this new, no-argument, read() function? Note that we would also delete the old read(I).

After summarizing the changes, write the complete new semantics.

2. The Semantics of Scheme

Explain the semantics of each of the following standard functions, given on pages 42 and 43 of the Scheme report.

a. cons

b. setcar

c. eqv

d. apply

3. Fibonacci Numbers

The Fibonacci sequence -- 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ... -- is defined recursively as

Unfortunately, the natural translation of this into program code leads to an algorithm with exponential running time. Fortunately, it is also possible to write an O(n) implementation iteratively, by keeping track of the previous two values and adding them at each iteration.

Write a O(n) Fibonacci function in FP. You may write this function iteratively or recursively. Make sure that your code is correct, working, and tested.

4. Extending FP

As many of you have noted, FP uses a somewhat different technique for describing the semantics of built-in functions and functional forms. In this question, you will further explore that technique.

Suppose we decided to add a case functional form to FP. Case has the form

case p f1 f2 ... fn

This is a true functional form. All arguments are functions, and it creates a new function. The informal meaning of case is ``apply p to the argument; if the result is 1, apply f1 to the argument; if the result is 2, apply f2; and so on and so forth''.

Write a formal definition of this new case functional form, using the same notation and care that Backus uses.


Disclaimer Often, these pages were created ``on the fly'' with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

This page may be found at

Source text last modified Fri Mar 12 12:58:08 1999.

This page generated on Fri Mar 12 13:00:11 1999 by SiteWeaver. Validate this page's HTML.

Contact our webmaster at