# Oz

Appeared in:
1991
Influenced by:
Typing discipline:
File extensions:
.oz
Versions and implementations (Collapse all | Expand all):
Programming language

Oz is a multi-paradigm programming language that provides features of both object-oriented and functional approaches.

## Elements of syntax:

Inline comments %% =, := fun{ ...} { a b ...} {} if condition then ... end if condition then ... else ... end for until: do ... end for i in 1..10 do ... end

## Examples:

### Hello, World!:

Example for versions Mozart 1.4.0
``````functor
import
Application
System

define
{System.showInfo 'Hello, World!'}
{Application.exit 0}

end
``````

### Factorial:

Example for versions Mozart 1.4.0

This example uses iterative factorial definition. Compile using `ozc -x <filename>`.

``````functor

import
Application System

define

local
F
in
F = {NewCell 1}
for I in 0..16 do
{System.showInfo {Append {Append {Int.toString I} "! = "} {Int.toString @F}}}
F := (I+1) * @F
end
{Application.exit 0}
end

end
``````

### Fibonacci numbers:

Example for versions Mozart 1.4.0

This example uses iterative definition of Fibonacci numbers.

``````functor

import
Application System

define

local
A B C S
in
A = {NewCell 0}
B = {NewCell 1}
C = {NewCell 0}
S = {NewCell ""}
for I in 1..16 do
C := @A + @B
A := @B
B := @C
S := {Append {Append @S {Int.toString @A}} ", "}
end
{System.showInfo {Append @S "..."}}
{Application.exit 0}
end

end
``````

### Fibonacci numbers:

Example for versions Mozart 1.4.0

This example uses tail-recursive definition of Fibonacci numbers.

``````functor

import
Application System

define
fun{Fib N}
fun{Loop N A B}
if N == 0 then
B
else
{Loop N-1 A+B A}
end
end
in
{Loop N 1 0}
end

local
S
in
S = {NewCell ""}
for I in 1..16 do
S := {Append {Append @S {Int.toString {Fib I}}} ", "}
end
{System.showInfo {Append @S "..."}}
{Application.exit 0}
end

end
``````

Example for versions Mozart 1.4.0

Oz is a strongly typed language, so all type conversions have to be done explicitly. Coefficients are converted in two steps (first to int, next to float) because converting string “1” into a float directly causes an error (“1.” converts into float just fine). `~` denotes unary minus.

``````functor

import
Application System Open

define

local
A B C D
class TextFile from Open.file Open.text end
StdIn = {New TextFile init(name:stdin)}
in
{System.showInfo "A = "}
A = {Int.toFloat {String.toInt {StdIn getS(\$)}}}
if A==0 then
{Application.exit 0}
end
{System.showInfo "B = "}
B = {Int.toFloat {String.toInt {StdIn getS(\$)}}}
{System.showInfo "C = "}
C = {Int.toFloat {String.toInt {StdIn getS(\$)}}}
D = B*B - 4.0*A*C
if D==0.0 then
{System.showInfo "x = "#{Float.toString ~0.5*B/A}}
{Application.exit 0}
end
if D>0.0 then
{System.showInfo "x1 = "#{Float.toString ~0.5*(B-{Sqrt D})/A}}
{System.showInfo "x2 = "#{Float.toString ~0.5*(B+{Sqrt D})/A}}
else
{System.showInfo "x1 = ("#{Float.toString ~0.5*B/A}#","#{Float.toString 0.5*{Sqrt ~D}/A}#")"}
{System.showInfo "x2 = ("#{Float.toString ~0.5*B/A}#","#{Float.toString ~0.5*{Sqrt ~D}/A}#")"}
end
{Application.exit 0}
end

end
``````