Warmup Exercises

These exercises provide an introduction to the language Oz and the Mozart platform. Everything can be evaluated by feeding lines in the Oz Programming Interface.

The chapters refer to chapters in the Oz tutorial.

1.1 Chapter 3

1.1.1 Browse, Show and variables

Declare a variable and bind it to a value. Show and Browse the variable. What's the difference?

Try to bind the variable to a new value. What happens, and why?

Write a more complex expression involving the variable and browse it. Also browse some constant.

Now declare a variable X and browse it without binding it. What happens and why?

Browse the expression X+17 and think about what the browser displays and why.

Declare another variable Y and bind X to it. What does the Browser display now?

Bind Y to a numeric value. What does the Browser display?

Now redeclare Y and bind it do a different value. Does the value of X change? Why/why not?

1.2 Chapter 4

1.2.1 Literals and variables

Declare X and bind it to Y.

Redeclare X and bind it to y (lower-case!). Explain why the behavior is different.

1.2.2 Records and tuples

Declare X and Y and bind X to foobar(gurka:Y pudding:0.57).

Browse X, X.gurka and X.pudding.

Bind Y to some value and watch the Browser. It should be pretty clear now what is happening, right? (If not, go back and redo Chapter 3.)

Redeclare X and Y, bind X to foobar(Y 0.57), browse X, X.1 and X.2 and bind Y to something.

1.2.3 Lists

Browse some simple list expressions, like 1|2|nil and [1 2].

Now form a list with an unbound variable tail 1|2|X and Browse it. See how the list becomes increasingly defined as you add elements by X=3|X2, and X2=4|X3 and so on.

1.2.4 Strings

Browse a string like "foo bar". (Note the double quotes, what happens if we instead use single quotes?). So what is a string in Oz, really? To display the string as a string in the Browser, do Options->Representation and check the box Strings. Then mark the list and do Selection->Rebrowse. Show a string. To show strings as text in the emulator, use System.showInfo instead of Show.

1.3 Chapter 5

1.3.1 Equality test operator (==) vs. equality (=)

Browse: 5 = 5, == 5, X = 5 and == 5 Try having X unbound, bound to 5 and bound to something different from 5. In particular, observe the difference between X = 5 and == 5 when X is unbound. So what is the difference between == and =?

1.3.2 Local variables and scoping

Feed:

declare X Y
{Browse X}
local Y in Y = 5 X = Y end

Observe X. Now Browse Y. Explain!

1.4 Chapter 6 and 7

1.4.1 Simple conditional

Feed:

declare X Y
{Browse Y}
if X == 5 then Y=foobar else Y=gurka end

What happens and why? Now feed X=5 and observe the result. Redo the experiment with X bound to something different than 5.

1.4.2 Procedures and functions

First think about what you expect to see in the Browser and then feed:

declare Max
proc{Max X Y Z}
   if X>then Z=X
   else Z=Y end 
end 
{Browse max({Max 1 2})}

Feed:

declare  
fun{Max X Y}
   if X<then Y
   else X end 
end

Try to predict what will happen if you feed:

{Browse {Max 3 4}}

{Browse {Max {Max 3 5} 4}}

What is the difference between fun and proc?

1.4.3 Pattern matching with case

Feed:

declare  
proc{Test X}
   case X of a|then {Show 'case'(1)}
   [] f(a) then {Show 'case'(2)}  
   [] [c b _] then {Show 'case'(3)}
   [] f(Y) then {Show 'case'(4)}
   [] Y | Z then {Show 'case'(5)}
   else {Show 'case'(6)} end 
end

Try to predict what will happen if you feed:

{Test [b c a]}
{Test f(b(3))}  
{Test f(a)}  
{Test [a b c]}  
{Test [c b f(a)]}
{Test Browse}

Note: 'case'(1) a.s.o. is written with quotes to distinguish from the keyword case.

Feed:

declare  
proc{Test X}
   case X of f(a Y c) then {Show 'case'(1)}
   else {Show 'case'(2)} end 
end

Try to predict what will happen if you feed:

declare X Y  
{Test f(X b Y)}  
 
declare X Y  
{Test f(a Y d)}
 
declare X Y
{Test f(X Y d)}

1.4.4 Procedures as values

Feed:

declare Max3 Max5
 
declare Max
proc{Max X Y Z}
   if X>then Z=X
   else Z=Y end 
end 
 
proc{SpecialMax Value SMax}
   proc{SMax X Z}
      if X>Value then Z=X
      else Z=Value end 
   end 
end 
 
{SpecialMax 3 Max3}
{SpecialMax 5 Max5}

Try to predict what will happen if you feed:

{Browse {Max3 4}#{Max5 4}}

1.4.5 Exceptions

Try to predict what will happen if you feed:

declare Max
proc{Max X Y Z}
   if X>then Z=X
   else Z=Y end 
end 
 
declare Y  
{Max 1 a Y}

Feed:

try 
   {Max 1 a Y}
catch X then 
   {Show caught_exception(X)}
end


Per Brand, Nils Franzén, Dragan Havelka, Erik Klintskog and Anna Neiderud
Version 1.2.0 (20010423)