Programming Languages 1: Test on Message Passing, Relational Model, and Programming Models, Exams of Programming Languages

The instructions and questions for a test in a programming languages 1 course, focusing on message passing, the relational model, and programming models. Students are required to identify correct statements, choose the best programming model for given problems, and write functions using oz's message passing model.

Typology: Exams

Pre 2010

Uploaded on 11/08/2009

koofers-user-rgj
koofers-user-rgj ๐Ÿ‡บ๐Ÿ‡ธ

9 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Fall, 2007 Name:
COP 4020 โ€” Programming Languages 1
Test on Message Passing, the Relational Model,
and Programming Models vs. Problems
Special Directions for this Test
This test has 6 questions and pages numbered 1 through 7.
This test is open book and notes.
If you need more space, use the back of a page. Note when you do that on the front.
Before you begin, please take a moment to look over the entire test so that you can budget your time.
Clarity is important; if your programs are sloppy and hard to read, you may lose some points. Correct syntax also
makes a difference for programming questions.
When you write Oz code on this test, you may use anything in the message passing or relational model (as in
chapters 5 and 9 of our textbook). The problem will say which model is appropriate. However, you must not use
imperative features (such as cells and assignment) or the library functions IsDet and IsFree.
You are encouraged to define functions or procedures not specifically asked for if they are useful to your program-
ming; however, if they are not in the Oz base environment, then you must write them into your test. (This means you
can use functions in the Oz base environment such as Map,FoldR,Filter,Append,Max, etc.)
For Grading
Problem Points Score
1 10
2 10
3 40
4 10
5 20
6 10
pf3
pf4
pf5

Partial preview of the text

Download Programming Languages 1: Test on Message Passing, Relational Model, and Programming Models and more Exams Programming Languages in PDF only on Docsity!

Fall, 2007 Name:

COP 4020 โ€” Programming Languages 1

Test on Message Passing, the Relational Model,

and Programming Models vs. Problems

Special Directions for this Test

This test has 6 questions and pages numbered 1 through 7.

This test is open book and notes.

If you need more space, use the back of a page. Note when you do that on the front.

Before you begin, please take a moment to look over the entire test so that you can budget your time.

Clarity is important; if your programs are sloppy and hard to read, you may lose some points. Correct syntax also

makes a difference for programming questions.

When you write Oz code on this test, you may use anything in the message passing or relational model (as in

chapters 5 and 9 of our textbook). The problem will say which model is appropriate. However, you must not use

imperative features (such as cells and assignment) or the library functions IsDet and IsFree.

You are encouraged to define functions or procedures not specifically asked for if they are useful to your program-

ming; however, if they are not in the Oz base environment, then you must write them into your test. (This means you

can use functions in the Oz base environment such as Map, FoldR, Filter, Append, Max, etc.)

For Grading

Problem Points Score

1. (10 points) Circle just the statement or statements below that are correct, and then briefly justify your answer.

(a) The message passing model can simulate cells like those found in the explicit state model.

(b) The message passing model is referentially transparent, because the message passing model does not

include any way to update state.

(c) The message passing model does not need to be defined using a mutable store, because none of its

primitives update any state.

(d) The Send primitive in the message passing model needs to be defined using mutable state, because it

updates the store variable that tracks the end of the stream.

2. (10 points) Circle just the statement or statements below that are correct, and then briefly justify your answer.

(a) The relational model is most useful when you know an algorithm for solving your problem and you have

a lot of time to implement it carefully.

(b) The relational model is most useful when you do not know an algorithm for solving your problem or you

do not have much time to implement such an algorithm carefully.

(c) The relational model is most useful when the problemโ€™s search space is very large or infinite.

(d) The relational model is most useful when the problemโ€™s search space is finite and not too large.

4. (10 points) Using Ozโ€™s message passing model, write a function NewGauge that takes no arguments and

returns a port object that stores an integer value in its state and that can respond to the messages up, down, and

fetch(Var). Initially the value stored in the port object is 0. The message up increments this value and

down decrements it. The fetch(Var) message binds Var to the current value. The following are some

examples, written using the Test function as in the homework.

declare MyGauge = {NewGauge} {Test local R in {Send MyGauge fetch(R)} R end โ€™==โ€™ 0} {Send MyGauge up} {Test {Send MyGauge fetch($)} โ€™==โ€™ 1} {Send MyGauge up} {Test {Send MyGauge fetch($)} โ€™==โ€™ 2} {Send MyGauge down} {Test {Send MyGauge fetch($)} โ€™==โ€™ 1} {Send MyGauge up} {Send MyGauge up} {Test {Send MyGauge fetch($)} โ€™==โ€™ 3}

MyGauge2 = {NewGauge} {Test {Send MyGauge2 fetch($)} โ€™==โ€™ 0} {Send MyGauge2 down} {Test {Send MyGauge2 fetch($)} โ€™==โ€™ ~1} {Send MyGauge2 down} {Test {Send MyGauge2 fetch($)} โ€™==โ€™ ~2} for I in 1..12 do {Send MyGauge2 up} end {Test {Send MyGauge2 fetch($)} โ€™==โ€™ 10}

You should use NewPortObject, from the textbook and homework, in your solution.

Please write your answer below.

\insert โ€™NewPortObject.ozโ€™ % assume this is already written

declare

5. (20 points) Using Ozโ€™s message passing model, write a function NewAuctioneer that takes no arguments

and returns a port object that acts as an auctioneer (an agent that is selling an item in an auction).

This port object understands several messages. The bid(Amt DidIWin) message places a bid on the item,

where Amt is a non-negative integer (the number of dollars bid) and DidIWin is an unbound store variable.

The stop message ends the auction. When the stop message is received, the DidIWin variable in the first

bid message received whose Amt was the largest is bound to true, and all other DidIWin variables in other

bid messages are bound to false. (Note that there may be no bids. In case of a tie, the first bid message

with the highest amount wins. If any bid message is received after the stop message, it does not win.)

The following are some examples, written using the Test function as in the homework.

declare MyAer = {NewAuctioneer} local Status1 Status2 Status3 Status4 in {Send MyAer bid(17 Status1)} {Send MyAer bid(8 Status2)} {Send MyAer bid(27 Status3)} {Send MyAer bid(24 Status4)} {Send MyAer stop} {Test Status1 โ€™==โ€™ false } {Test Status2 โ€™==โ€™ false } {Test Status3 โ€™==โ€™ true } {Test Status4 โ€™==โ€™ false } end

MyAer2 = {NewAuctioneer} local Status1 Status2 Status3 Status Status5 Status6 Status in {Send MyAer2 bid(100 Status1)} {Send MyAer2 bid(15 Status2)} {Send MyAer2 bid(99 Status3)} {Send MyAer2 bid(99 Status4)} {Send MyAer2 bid(100 Status5)} {Send MyAer2 bid(100 Status6)} {Send MyAer2 bid(100 Status7)} {Send MyAer2 stop} % first of the highest bids wins {Test Status1 โ€™==โ€™ true } {Test Status2 โ€™==โ€™ false } {Test Status3 โ€™==โ€™ false } {Test Status4 โ€™==โ€™ false } {Test Status5 โ€™==โ€™ false } {Test Status6 โ€™==โ€™ false } {Test Status7 โ€™==โ€™ false } end

% Stopping with no bids MyAer3 = {NewAuctioneer} {Send MyAer3 stop} % Bidding after an auction is over {Test {Send MyAer3 bid(50 $)} โ€™==โ€™ false }

You should use NewPortObject, from the textbook and homework, in your solution.

Hint: you may want to use helping functions or procedures.

Please write your answer in the space provided on the next page.

6. (10 points) Using Ozโ€™s relational model, write a procedure PositionOf such that

{PositionOf Ls E Index} succeeds when Ls is a list in which the element E occurs at the Index

position (counting from 1) and fails otherwise. The following are examples, using the standard SolveAll and

SolveFirst functions described in class.

{Test {SolveAll proc {$ Ans} {PositionOf nil 0 Ans} end } โ€™==โ€™ nil} {Test {SolveAll proc {$ Ans} {PositionOf 0|nil 0 Ans} end } โ€™==โ€™ [1]} {Test {SolveFirst proc {$ Ans} {PositionOf [4 0 2 0] 4 Ans} end } โ€™==โ€™ 1} {Test {SolveAll proc {$ Ans} {PositionOf [4 0 2 0] 0 Ans} end } โ€™==โ€™ [2 4]} {Test {SolveFirst proc {$ Ans} {PositionOf [4 0 2 0] 2 Ans} end } โ€™==โ€™ 3} {Test {SolveAll proc {$ Ans} {PositionOf [4 0 2 0] 5 Ans} end } โ€™==โ€™ nil} {Test {SolveAll proc {$ Ans} {PositionOf [5 0 2 1 4 0 2 0 88] 0 Ans} end } โ€™==โ€™ [2 6 8]} {Test {SolveFirst proc {$ X} {PositionOf [99 22 X 55] 7 3} end } โ€™==โ€™ 7} {Test {SolveFirst proc {$ Ind} {PositionOf [99 22 8 55] Ind 3} end } โ€™==โ€™ 8}