| CS 330 | Home | Schedule | Resources |
Estimated time required: 4.5 hours
Submission: Please submit the answers to the questions as a plain text file attachment to the TA email account (CS330NOtaSPAM@gmail.com, remove "NO SPAM"). Please name the file as 'a1' followed by your name (last name first). For example 'a1gearyirene'.
The objective of this assignment is to familiarize you with
Start
the Mozart interface. On the CS Lab computers Mozart is pre-installed.
Just type
"oz < filename.oz>   &" at the command prompt. Your Oz programs should be saved in a file ending in '.oz' to get automatic Oz-style syntax highlighting and formatting. The ampersand will cause Oz to run in the background, and free up the command prompt so it can be used to run other commands.
Consult this link for help on compilation and formatting under Mozart.
Quick hints:
Here some exercises to familiarize you with the environment and the language.
Compiling and running. (5 points) Type the following program.
{Browse 1}
{Browse 2}
{Browse 3}
{Browse 4}
This program calls the procedure Browse four times. This
procedure displays the value of its argument.
Arithmetic expressions. (5 points) We can use the procedure Browse
to evaluate arithmetic expressions. For example:
{Browse (1+5)*(9-2)}
Calculate
Caution: the negation operator (for negative numbers) is a tilde(~).
Submit the answers to the calcuations.
Variables. (5 points) The keyword declare lets
you create a variable and optionally assign a value to it. As in the
preceding exercise, the value can be given by an expression. For
example:
declareOne can then use the name of the variable as synonym of the value. For example:
X=(6+5)*(9-7)
{Browse X}
{Browse X+5}
Redo the calculations of Exercise 2, by using variables where you
consider it useful. Submit the code you write.
Multiple declarations. (10 points) Consider the following program:
declareAnything after the sign
X=42
Z=~3
{Browse X} % (1)
{Browse Z}
declare
Y=X+5
{Browse Y} % (2)
declare
X=1234567890
{Browse X} % (3)
% is
ignored by the compiler. That makes it possible to comment the
program. Submit your answers to the following questions.
X always
accessible? At best guess, is its value always in memory?
What about Z? (1) .
Which X is displayed? When was X bound to
this value? (2) .
Was the value of Y affected by the second declaration
of X? Why? Boolean expressions. (5 points) In addition to arithmetic
expressions, OZ allows Boolean expressions. The two possible values of
such expressions are true and false.
Thus, the comparison of two integer values returns Boolean.
{Browse 3 == 7} % equalsExecute these comparison tests and submit the returned Boolean values.
{Browse 3 \= 7} % not equals
{Browse 3 < 7} % less than
{Browse 3 =< 7} % less than or equal
{Browse 3 > 7} % greater than
{Browse 3 >= 7} % greater than or equal
Functions and expressions. (20 points) We can also use function calls in arithmetic and Boolean expressions. Here's an example:
{Browse {Max 3 7}}
{Browse {Not 3==7}}
The function Max takes two arguments and returns the
greater value. The function Not takes a Boolean
argument and returns its negation. Note that the arguments of the
functions are also arithmetic or Boolean expressions.
Use the function Max in an expression which
returns the maximum of three numbers X Y
and Z. Test with X=7 Y=5 Z=6.
Also test with other values and submit both your code and test cases.
In the following program, the sub-expression {SlowAdd 1000 1} is evaluated three times. The function SlowAdd
calculates a sum in at least a second (1000
milliseconds). Transform the expression in the call Browse so that it performs an equivalent computation but only
calls SlowAdd once, by using a variable to memoize the result of the call. Hint: Try introducing
a variable with local ... in ... end within the call to Browse that saves the result of one call to SlowAdd and reuses it. Submit the modified line of code.
declare
fun {SlowAdd X Y}
{Delay 1000} X+Y
end
{Browse {SlowAdd 1000 1}+{SlowAdd 1000 1}+{SlowAdd 1000 1}}
Write a function which returns the sign of an integer N
passed as an argument. The possible values of the sign are 0
1 and ~1 according to
whether N is zero, positive or negative. Test your
function for various values of N. Submit both code and tests.
declare
X=3
Y=if X>4 then X+1 else X-1 end
{Browse Y}
declare
X
proc {Foo Y} Y=3 end
{Browse X}
{Delay 3000}
{Show X}
{Foo X}
{Show X} In the following program, what is the scope of each declared identifier? Or, in other words, to which declaration does each identifier occurrence refer?
local P Q X Y Z in % (1)
fun {P X}
X*Y+Z % (2)
end
fun {Q Y}
X*Y+Z % (3)
end
X=1
Y=2
Z=3
{Browse {Q {P 4}}}
end
Using the above information, predict the result of running the program. If necessary, carry out the program manually, by noting the instructions actually carried out and their environment.
Modify the code so that equation 2 multiplies the local function parameter with the variable X on line (1) then adds the result to Z from line (1).
Check that the modified program displays 10.
Make a similar modification so that equation (3) multiplies the local function parameter with the variable X on line (1) then adds it to the variable Y defined on line (1). Check that the modified program posts 9.
The procedural abstraction. (20 points) In this exercise, you will explore the potential of functions to better understand them. Consider the following program.
declare
X=3
{Browse X+2} % (1)
{Browse X*2} % (2)
Write a function Add2 with no arguments that computes
X + 2. Then, replace the
expression within the call to Browse on line (1) with a
call to the function Add2.
Now write a function Mul2 that computes X * 2. This function will take X as a
parameter.
As in the preceding point, replace the expression within the call to Browse on line (2) by a call to the function Mul2.
Describe the functions Add2 and Mul2.
What is their contextual environment, or in other words, their free identifiers and their value?
Redeclare the identifier X with the
instruction below, without redefining the procedures Add2
and Mul2.
declareWhat happens now if lines 1 and 2 alone are re-executed so that the functions
X=4
Add2 and Mul2 are
called again?
Submission: Please submit the answers to the questions as a plain text file attachment to the TA email account (CS330NOtaSPAM@gmail.com, remove "NO SPAM"). Please name the file as 'a1' followed by your name (last name first). For example 'a1gearyirene'.
The file chapter1.oz contains examples of OZ code. Compile and run some of these examples.
Calculate the following expressions using only the *, +, and - operators by using variables in an intelligent way (Hint: consider 2 7 = 2*2*2*2*2*2*2 ).
2 7 * 3 + 2 42 - 2 100 * 5
The function Sqrt returns the square root of the
real number passed in as an argument. Use it to build a function Distance
which calculates the distance between two points of a plane. This
function
takes four real numbers as arguments.
local
X1=3. Y1=2.
X2=2.5 Y2=0.
in
{Browse {Distance X1 Y1 X2 Y2}}
end
Build a function Equal which takes two arguments
and
returns true if they are equal,
false if not.