Skip to content

Instantly share code, notes, and snippets.

@jvrsgsty
Created March 23, 2014 18:57
Show Gist options
  • Save jvrsgsty/9727868 to your computer and use it in GitHub Desktop.
Save jvrsgsty/9727868 to your computer and use it in GitHub Desktop.
Oz Tutorial
{Browse 'Hello World'}
declare W H
{Browse foo(width:W height:H surface:thread W*H end)}
W=3
H=5
declare
proc {Ints N Xs}
or N = 0 Xs = nil
[] Xr in
N > 0 = true Xs = N|Xr
{Ints N-1 Xr}
end
end
local
proc {Sum3 Xs N R}
or Xs = nil R = N
[] X|Xr = Xs in
{Sum3 Xr X+N R}
end
end
in proc {Sum Xs R} {Sum3 Xs 0 R} end
end
local N S R in
thread {Ints N S} end
thread {Sum S R} {Browse R} end
%N = 1000
declare X Y
proc {FullAppend L1 L2 L3}
dis L1=nil L2=L3 [] X M1 M3 in
L1=X|M1 L3=X|M3 {FullAppend M1 L2 M3}
end
end
proc {Q A}
X Y
in
{FullAppend X Y [1 2 3 4]}
A = X#Y
end
local
S={New Search.object script(Q)}
in
{Browse {S next($)}}
end
local A B C in
A = 3
D = 10
B = A
A = C
{Show [A B C]}
end
local X Y Z in
f(1:X 2:b) = f(a Y)
f(Z a) = Z
{Browse [X Y Z]}
end
% See, lists are just tuples, which are just records
local L1 L2 L3 Head Tail in
L1 = Head|Tail
Head = 1
Tail = 2|nil
L2 = [1 2]
{Browse L1==L2}
L3 = '|'(1:1 2:'|'(2 nil))
{Browse L1==L3}
end
local A B in
A = 3
proc {B}
{Show A + 'Tinman'}
end
{B 7}
end
local
proc {MForAll Xs P}
case Xs
of nil then skip
[] X|Xr then
{P X}
{MForAll Xr P}
end
end in
{MForAll [ 1 2 3 4] Browse}
end
{Show 'Hello world'}
% What is a binary tree?
local
proc {And B1 B2 ?B}
if B1 then B = B2 else B = false end
end
in
proc {BinaryTree T ?B}
case T
of nil then B = true
[] tree(K V T1 T2) then
{And {BinaryTree T1} {BinaryTree T2} B}
else B = false end
end
end
%Checking a binary tree lazily
local
proc {AndThen BP1 BP2 ?B}
if {BP1} then B = {BP2} else B = false end
end
in
proc {BinaryTree T ?B}
case T
of nil then B = true
[] tree(K V T1 T2) then
{AndThen
proc {$ B1} {BinaryTree T1 B1} end
proc {$ B2} {BinaryTree T2 B2} end
B}
else B = false end
end
end
proc {Insert Key Value TreeIn ?TreeOut}
if TreeIn == nil then TreeOut = tree(Key Value nil nil)
else
local tree(K1 V1 T1 T2) = TreeIn in
if Key == K1 then TreeOut = tree(Key Value T1 T2)
elseif Key < K1 then
local T in
TreeOut = tree(K1 V1 T T2)
{Insert Key Value T1 T}
end
else
local T in
TreeOut = tree(K1 V1 T1 T)
{Insert Key Value T2 T}
end
end
end
end
end
% case for pattern matching
proc {Insert Key Value TreeIn ?TreeOut}
case TreeIn
of nil then TreeOut = tree(Key Value nil nil)
[] tree(K1 V1 T1 T2) then
if Key == K1 then TreeOut = tree(Key Value T1 T2)
elseif Key < K1 then T in
TreeOut = tree(K1 V1 T T2)
{Insert Key Value T1 T}
else T in
TreeOut = tree(K1 V1 T1 T)
{Insert Key Value T2 T}
end
end
end
proc {SMerge Xs Ys Zs}
case Xs#Ys
of nil#Ys then Zs=Ys
[] Xs#nil then Zs=Xs
[] (X|Xr) # (Y|Yr) then
if X=<Y then Zr in
Zs = X|Zr
{SMerge Xr Ys Zr}
else Zr in
Zs = Y|Zr
{SMerge Xs Yr Zr}
end
end
end
local
proc {Ping N}
if N==0 then {Browse 'ping terminated'}
else {Delay 500} {Browse ping} {Ping N-1} end
end
proc {Pong N}
{For 1 N 1
proc {$ I} {Delay 600} {Browse pong} end }
{Browse 'pong terminated'}
end
in
{Browse 'game started'}
thread {Ping 10} end
thread {Pong 10} end
end
% Polynomials
% Javier Sagastuy Brena
% 190314
% ITAM
%
% Start off declaring global variables we will use later
declare P1 P2 P3 P4 P Q1 Q2 Q R S T
% And declare all the procedures we will be using
% Fills with zeros to the left
proc {ZeroPad C N ?Z}
case N
of 0 then Z = C
[] N1 then Z = 0|{ZeroPad C N1-1}
end
end
% creates a list of N zeros
proc {Zeros N ?Z}
case N
of 0 then Z = nil
[] N1 then Z = 0|{Zeros N1-1}
end
end
% Appends List L2 to list L1
proc {Append L1 L2 ?L3}
case L1
of nil then L2=L3
[] X|M1 then L3=X|{Append M1 L2} end
end
% Fills with zeros to the right
proc {ZeroFill P N ?Z}
Z = {Append P {Zeros N}}
end
% Calcultes the degree of a polynomial
proc {Degree P ?R}
case P
of nil then R = ~1
[] H|T then R = {Degree T}+1
end
end
% Creates a monomial with coefficient C and degree D
proc {Monomial C D ?M}
M = {ZeroFill [C] D}
end
% Verifies if a polynomial is zero
proc {IsZero P ?B}
case P
of nil then B = true
[] H|T then B = {And H==0 {IsZero T}}
end
end
% Adds two polynomias of the same degree
proc {AddP P1 P2 ?R}
case P1
of nil then R = nil
[] H1|T1 then H2 T2 in P2 = H2|T2 R = H1+H2|{AddP T1 T2}
end
end
% Polynomial Addition
proc {Add P1 P2 ?R} D1 D2 in
D1 = {Degree P1}
D2 = {Degree P2}
or
D1 == D2 = true then R = {AddP P1 P2}
[] D1 > D2 = true then R = {AddP P1 {ZeroPad P2 D1-D2}}
[] D1 < D2 = true then R = {AddP {ZeroPad P1 D2-D1} P2}
end
end
% Multiplies every term of the polynomial by -1
proc {Minus P ?R}
case P
of nil then R = nil
[] H|T then R = ~H|{Minus T}
end
end
% Polynomial Subtraction
proc {Subtract P1 P2 ?R}
R = {Add P1 {Minus P2}}
end
% Polynomial Multiplication
proc {Mult P1 P2 ?R} D in
D = {Degree P1}
case P1
of nil then R = nil
[] H|T then R = {Add {Times P2 H D} {Mult T P2}}
end
end
% Multiplies a polynomial by the term H*X^N
proc {Times P H N ?R}
case P
of nil then R = {Zeros N}
[] H1|T1 then R = H*H1|{Times T1 H N}
end
end
% Auxiliary function for the composition
proc {CompInt P Q C I N ?R}
case I
of 1 then T in
T = {Monomial {List.nth P N-I+1} 0}
R = {Add T {Mult Q C}}
[] N1 then T AUX in
T = {Monomial {List.nth P N-I+1} 0}
AUX = {Add T {Mult Q C}}
R = {CompInt P Q AUX I-1 N}
end
end
% Polynomial Composition p(q(x))
proc {Comp P Q ?R} C I in
I = {Degree P}
C = {Monomial 0 0}
R = {CompInt P Q C I+1 I+1}
end
% Polynomial Evaluation
proc {Eval P X ?V} N in
N = {Degree P}
case P
of nil then V = 0
[] H|T then V = H*{Pow X N} + {Eval T X}
end
end
% Polynomial Derivation
proc {Diff P ?D} N in
N = {Degree P}
case P
of [X] then D = nil
[] H|T then D = H*N|{Diff T}
end
end
% Writes the polynomial as a virtual string
proc {ToString P ?S} N H T in
P = H|T
N = {Degree P}
if {IsZero P} then
S = "0"
else S = {ToStringP P}
end
end
% Writes the non zero polynomials as a virtual String
proc {ToStringP P ?S} N H T in
P = H|T
N = {Degree P}
case N
of 0 then
or
H == 0 = true then S = ""
[] H>0 = true then S = " +"#H
[] H<0 = true then S = " "#H
end
[] 1 then
or
H == 0 = true then S = ""#{ToString T}
[] H>0 = true then S = " +"#H#"x"#{ToString T}
[] H<0 = true then S = " "#H#"x"#{ToString T}
end
[]N1 then
or
H == 0 = true then S = ""#{ToString T}
[] H>0 = true then S = " +"#H#"x^"#N1#{ToString T}
[] H<0 = true then S = " "#H#"x^"#N1#{ToString T}
end
end
end
in
% The main Statement in the program
{Monomial 4 3 P1}
{Monomial 3 2 P2}
{Monomial 1 0 P3}
{Monomial 2 1 P4}
P = {Add P1 {Add P2 {Add P3 P4}}}
{Monomial 3 2 Q1}
{Monomial 5 0 Q2}
Q = {Add Q1 Q2}
R = {Add P Q}
S = {Mult P Q}
T = {Comp P Q}
% Prints the test cases results
{Browse {ToString {Monomial 0 0}}}
{Browse {ToString {Subtract P P}}}
{Browse {ToString P}}
{Browse {ToString Q}}
{Browse {ToString R}}
{Browse {ToString S}}
{Browse {ToString T}}
{Browse {ToString {Subtract {Monomial 0 0} P}}}
{Browse {Eval P 3}}
{Browse {ToString {Diff P}}}
{Browse {ToString {Diff {Diff P}}}}
local Max X Y Z in
proc {Max X Y Z}
if X >= Y then Z = X else Z = Y end
end
X = 5
Y = 10
{Max X Y Z} {Browse Z}
end
local
Max = proc {$ X Y Z}
if X >= Y then Z = X
else Z = Y end
end
X = 5
Y = 10
Z
in
{Max X Y Z} {Browse Z}
end
declare T I Y LT RT W in
T = tree(key:I value:Y left:LT right:RT)
I = seif
Y = 43
LT = nil
RT = nil
W = tree(I Y LT RT)
{Browse [T W]}
% Operators
{Browse T.key}
{Browse W.1}
local X in {Arity T X} {Browse X} end
local X in {Arity W X} {Browse X} end
local X in {CondSelect W key eeva X} {Browse X} end
local X in {CondSelect T key eeva X} {Browse X} end
local S in
{AdjoinList tree(a:1 b:2) [a#3 c#4] S}
{Show S}
end
% Lists
{Show 1|2|3|nil}
{Show [1 2 3]}
{Show "Hello"}
local X = 1 in
local X = 2 in
local X = 3 in
{Show X}
end
{Show X}
end
{Show X}
end
declare X0 X1 X2 X3 in
thread
local Y0 Y1 Y2 Y3 in
{Browse [Y0 Y1 Y2 Y3]}
Y0 = X0+1
Y1 = X1+Y0
Y2 = X2+Y1
Y3 = X3+Y2
{Browse completed}
end
end
{Browse [X0 X1 X2 X3]}
X0 = 0
X1 = 1
X2 = 2
X3 = 3
local X in
X = 1
{Show X}
%X = 4
end
local I F C in
I = 5
F = 5.5
C = &t
{Browse [I F C]}
end
local X Y B in
X = foo
{NewName Y}
B = true
{Browse [X Y B]}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment