Oz (langage) — Wikipédia
OzCheatSheet
- Dataflow
Variables¶
upp
local X, Y in
X = 6
end
declare # Creates a link identifier (name) <-> variable(value).
X = 2 + 8
oz title:"List Tupples and Records"
<List T> ::= nil | T ‘|’ <List T> [EBNF grammar rule]
List = [ 1 2 3 ] = 1|2|3|nim
circle(1:0 2:1 3:3 4:blue 5:dots) % tupple
Tupple = jasim('a' 3 neato) % Tuple of three values
Record = circle(x:0 y:3 radius:3 color:blue style:dots) % record
Tupple.2 == Record.y
oz title:concurrencefun {Map List F}
case List of nil then nil
[] H|T then thread {F H} end | {Map T F}
end
declare F
{Browse {Map [1 2] F}} % [_ _]
F = fun {$ X} X+1 end % [2 3]
```oz title:"Sum of list elements (naïve and with accumulator)"
fun {Sum L}
if L==nil then 0
else {Head L} + {Sum {Tail L}}
end end
fun {Sum2 L A}
if L==nil then A
else {Sum2 {Tail L} A+{Head L}}
end end
```oz title:"Return the nth element of L"
fun {Nth L N}
if N==1 then {Head L}
elseif N>1 then {Nth {Tail L} N-1}
end end
Trees¶
Inserting A new key in an ordered binary
fun {Lookup K T}
case T
of leaf then notfound
[] tree(key:Y value:V T1 T2) andthen K==Y then
found(V)
[] tree(key:Y value:V T1 T2) andthen K<Y then
{Lookup K T1}
[] tree(key:Y value:V T1 T2) andthen K>Y then
{Lookup K T2}
end
end
fun {Insert K W T}
case T
of leaf then tree(key:K value:W leaf leaf)
[] tree(key:Y value:V T1 T2) andthen K==Y then
tree(key:K value:W T1 T2)
[] tree(key:Y value:V T1 T2) andthen K<Y then
tree(key:Y value:V {Insert K W T1} T2)
[] tree(key:Y value:V T1 T2) andthen K>Y then
tree(key:Y value:V T1 {Insert K W T2})
end
end
Tuples¶
Operations on tuples
{Label X}
returns the label of tuple X
. The label is a constant, called an atom.
{Width X}
returns the number of fields.
Accessing fields
Fields are numbered from 1
up to {Width X}
.
X.N
returns the nth field of tuple X
:
Building a tree
A tree can be built with tuples:
Testing equality
Equality testing with a number or atom → the number or atom must be the same.
Equality testing of trees → the two trees must have the same root tuples and the same subtrees in corresponding fields. Careful when the tree has a cycle !
A list is a tuple¶
The list H|T
is actually a tuple ‘|’(H T)
. → it is the same thing in the kernel language but for programers comfort there is a syntactic sugar for writing list.
Syntax of lists as tuples
Record¶
[!INFO] A record is a generalization of a tuple. Field names can be atoms or any integer. Does not have to start with 1 and does not have to be consecutive. A record also has a label and a width.
The position of a field is no longer meaningful. Instead, it is the field name that is meaningful
Operations on records
{Label X}=state
,{Width X}=3
andX==state(a:1 b:2 2:a)
.
{Arity X}
returns a list of field names → [2 a b] (in lexicographic order). Also works for tuples and lists.
A tuple is a record ⇒ The kernel language contains only records.
The record:X = state(1:a 2:b 3:c)
is the same as the tuple:X = state(a b c)
[!INFO] The contextual environment of a function / procedure contains all the identifiers that are used inside the function but declared outside of the function.
[!INFO] A free identifier of an instruction is an identifier used inside the instruction that is declared outside the instruction.
Procedure arguments are not free because the argument defines the identifier