tutorial9

advertisement
Survey of Programming Language Concepts, COSC-3308-01
Tutorial 9
Exercise 1. (Computing Maximum with Fold) Compute the maximum element from a
list of numbers by folding. What is the initial value to choose for passing to FoldL or
FoldR (remember: there is no smallest integer)? Which version of folding are you using
(FoldL or FoldR)? Why?
Exercise 2. (Mapping Tuples) Develop a function {MapTuple T F} that returns a
tuple that has the same width and label as the tuple T with its fields mapped by the
function F. For example,
local
fun {Sq X} X*X end
in
{MapTuple a(1 2 3) Sq}
end
should return a(1 4 9).
Hint: A tuple is constructed with {MakeTuple L N}, where L is the label and N is the
width of the tuple.
Exercise 3. (Executing Threads) Execute the following example with help of the
abstract machine.
local A B C in
thread if A then B=true else B=false end end
thread if B then C=false else C=true end end
A=false
end
Just sketch execution, the important point here is to understand how threads are created
and executed.
Exercise 4. (Threads) Give the values for the variables after execution has terminated:
thread if X==1 then Y=2 else Z=2 end end
thread if Y==1 then X=1 else Z=2 end end
X=1
and also for:
thread if X==1 then Y=2 else Z=2 end end
thread if Y==1 then X=1 else Z=2 end end
X=2
Exercise 5. (call by value and call by reference) Explain what and why the following
Oz program will display:
declare
proc {F A}
A:=@A+1
A:=@A*@A
end
proc {G A}
E={NewCell A}
in
E:=@E+1
E:=@E*@E
end
local
C={NewCell 0}
D={NewCell 1}
in
C:=5
D:=6
{Browse @C#@D}
{F C}
{G @D}
{Browse @C#@D}
end
Exercise 6. (object-oriented programming) Write a class that specify the characteristics
of a car, like type (sedan, jeep, mini, SUV, etc), gear (auto, manual), maximum speed
(mph), average fuel consumption (mpg), etc. Create few objects to illustrate your desired
cars.
Download