CS 330 Home Schedule Resources

Assignment 1
First steps in OZ

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

You will write small programs and run them under Mozart.

Set Up

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:


Exercises

Here some exercises to familiarize you with the environment and the language.

  1. 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.

  2. 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.

  3. 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:

         declare
    X=(6+5)*(9-7)
    One can then use the name of the variable as synonym of the value. For example:
        {Browse X}
    {Browse X+5}
    Redo the calculations of Exercise 2, by using variables where you consider it useful. Submit the code you write.

  4. Multiple declarations. (10 points) Consider the following program:

       declare
    X=42
    Z=~3
    {Browse X} % (1)
    {Browse Z}

    declare
    Y=X+5
    {Browse Y} % (2)

    declare
    X=1234567890
    {Browse X} % (3)
    Anything after the sign % is ignored by the compiler. That makes it possible to comment the program. Submit your answers to the following questions.

  5. 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}     % equals
    {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
    Execute these comparison tests and submit the returned Boolean values.

  6. 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.

  7. Parameter-passing behavior (10 points) Consider the following fragment of code. What is shown in the emulator window when it is executed? (Toggle the appearance of the emulator window using the Show/Hide option in the Oz menu before running the code.) How would you replicate the effect on X in C++? (In other words, how could you implement the same parameter passing behavior in C++?)
       declare
    X
    proc {Foo Y} Y=3 end
    {Browse X}
    {Delay 3000}
    {Show X}
    {Foo X}
    {Show X}
  8. Lexical scope and environment. (20 points) Remember, the scope of a declaration is the section of a program where an identifier is defined and corresponds to this declaration. The environment at a given time of the execution is the whole set of the definite identifiers and their respective variables in memory. Submit your answers to each question below.

  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)


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'.

Optional additional exercises

  1. The file chapter1.oz contains examples of OZ code. Compile and run some of these examples.

  2. 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

  3. 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

  4. Build a function Equal which takes two arguments and returns true if they are equal, false if not.