Final Examination

This is a take-home examination. Return your exam to me (RTH) by May 9th. 1996 before 5:00 pm. You may consult with others in the class ONLY IF you indicate their name(s) on your answer sheets. Points may be lost if your answers show similarities with others and you have not stated your partners. Answer ALL the questions.

Question 1. Imperative languages.

Write the simplest possible Pascal program that could produce the following activation record structure. Do not worry about types of variables or parameters; their names are important, however. Indicate in your code the point of execution that produces the structure.

Question 2. Object-oriented languages.

Translate the Smalltalk class definition below into C++. The C++ version will have to reflect typing choices, whereas the Smalltalk version does not declare any types. Make sure you indicate these choices clearly. !...! is a comment in this version of the Smalltalk syntax. The method rem: is the equivalent of the modulus function; the method quo: does integer division.

Object subclass : Amount
  instanceVariableNames: 'totalCents'
  classVariableNames: ''

!Amount class method!

Dollars: d cents: c
  self new dollars: d cents: c

!Amount instance methods!
* aValue
  | newTotalCents |
  newTotalCents ( totalCents * aValue.
  ( Amount Dollars: (newTotalCents rem: 100)
           Cents: (newTotalCents quo: 100)

+ anAmount
  | newTotalCents |
  newTotalCents ( totalCents + anAmount totalCents.
  ( Amount Dollars: (newTotalCents rem: 100)
           Cents: (newTotalCents quo: 100)

- anAmount
  | newTotalCents |
  newTotalCents ( totalCents - anAmount totalCents.
  ( Amount Dollars: (newTotalCents rem: 100)
           Cents: (newTotalCents quo: 100)

> anAmount
  ( totalCents > (anAmount totalCents)

cents
  ( totalCents rem: 100

dollars
  ( totalCents quo: 100

dollars: d cents c:
  totalCents ( 100 * d + c

totalCents
  ( totalCents

Question 3. Functional languages.

Below is a Scheme program. In a sentence, what does it do? Your answer must include a trace of function calls, similar to the ones used in class, for the call (mystery 5). It is NOT sufficient to just run it and report the result - you must show how the function works

(define (mystery n)
(let ((f1 (lambda (fi) (fi fi 1 1)))
(f2 (lambda (fi p c)
(if (> count n)
prod
(fi fi (* c p) (+ c 1))))))
(f1 f2)))

Question 4. Logical Langauges.

Below is a Prolog program about cats. Describe what it does in one sentence, with the query:

?- p(X).
Your answer must include a Prolog search tree similar in appearance to the ones in the textbook. The built-in precicate asserta adds its argument to the front of the database. It can be resatisfied, although its side-effect (changing the database) cannot be undone. Likewise retract permanently removes its argument from the database. The built-in predicate call tries to satisfy its argument as a goal; the infix predicate \== is 'not equal'. The built-in predicate fail forces backtracking. It is NOT suffcient to just run it and report the result - you must show that you understand how it works.

p(L) :- q(X,cat(X),L).
q(X,G,_) :-
  asserta(x(0)),
  call(G),
  asserta(x(X)),
  fail.
q(_,_,L) :-
  g([],M),
  !,
  L = M.
g(S,L) :-
  n(X),
  !,
  g([X|S],L).
g(L,L).
n(X) :-
  retract(x(X)),
  !,
  X \== 0.

cat(andy).
cat(mandy).