Mozart 1.4.0

Version of implementation Mozart of programming language Oz

A version of Mozart, released on July 3, 2008.

Examples:

Hello, World! - Oz (262):

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

Factorial - Oz (263):

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 - Oz (264):

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 - Oz (265):

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

Quadratic equation - Oz (266):

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 
      {System.showInfo "Not a quadratic equation."}
      {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