Content deleted Content added
Citation bot (talk | contribs) Alter: template type, journal. Add: year, isbn, doi. | You can use this bot yourself. Report bugs here. | Activated by Headbomb | via #UCB_webform |
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags |
||
Line 51:
* Tuples: Records with integer features in ascending order: <code> circle(1:0 2:1 3:3 4:blue 5:dots) </code>.
* Lists: a simple linear structure
<
'|'(2 '|'(4 '|'(6 '|'(8 nil)))) % as a record.
2|(4|(6|(8|nil))) % with some syntactic sugar
2|4|6|8|nil % more syntactic sugar
[2 4 6 8] % even more syntactic sugar
</syntaxhighlight>
Those data structures are values (constant), [[first-class object|first class]] and [[dynamic typing|dynamically type checked]]. Variable names in Oz start with an uppercase letter to distinguish them from [[Literal (computer programming)|literals]]<ref>https://mozart.github.io/mozart-v1/doc-1.4.0/tutorial/node3.html#label18</ref> which always begin with a lowercase letter.
Line 70:
| url-status = dead
}}</ref> are first class values, allowing [[Higher-order programming|higher order functional]] programming:
<
fun {Fact N}
if N =< 0 then 1 else N*{Fact N-1} end
end
</
fun {Comb N K}
{Fact N} div ({Fact K} * {Fact N-K}) % integers can't overflow in Oz (unless no memory is left)
Line 84:
end
end
</syntaxhighlight>
Functions may be used with both free and bound variables. Free variable values are found using static [[Scope (computer science)|lexical scoping]].<ref name="Scoping">
{{cite journal
Line 96:
====Higher-order programming====
Functions are like other Oz objects. A function can be passed as an attribute to other functions or can be returned in a function.
<
fun {Square N} % A general function
N*N
Line 110:
%usage
{Browse {Map Square [1 2 3]}} %browses [1 4 9]
</syntaxhighlight>
====Anonymous functions====
Line 116:
In the following, the square function is defined anonymously and passed, causing <code>[1 4 9]</code> to be browsed.
<
{Browse {Map fun {$ N} N*N end [1 2 3]}}
</syntaxhighlight>
Since anonymous functions don't have names, it is not possible to define recursive anonymous functions.
Line 124:
====Procedures====
Functions in Oz are supposed to return a value at the last statement encountered in the body of the function during its execution. In the example below, the function Ret returns 5 if X > 0 and -5 otherwise.
<
declare
fun {Ret X}
if X > 0 then 5 else ~5 end
end
</syntaxhighlight>
But Oz also provides a facility in case a function must not return values. Such functions are called procedures.<ref>https://mozart.github.io/mozart-v1/doc-1.4.0/tutorial/node5.html#control.procedure</ref> Procedures are defined using the construct "proc" as follows
<
declare
proc {Ret X}
if X > 0 then {Browse 5} else {Browse ~5} end
end
</syntaxhighlight>
The above example doesn't return any value, it just prints 5 or -5 in the Oz browser depending on the sign of X.
===Dataflow variables and declarative concurrency ===
When the program encounters an unbound variable it waits for a value. For example, below, the thread will wait until both X and Y are bound to a value before showing the value of Z.
<
thread
Z = X+Y
Line 148:
thread X = 40 end
thread Y = 2 end
</syntaxhighlight>
The value of a dataflow variable cannot be changed once it is bound:
<
X = 1
X = 2 % error
</syntaxhighlight>
Dataflow variables make it easy to create concurrent stream agents:
<
fun {Ints N Max}
if N == Max then nil
Line 179:
{Browse Y}
end
</syntaxhighlight>
Because of the way dataflow variables work, it is possible to put threads anywhere in a program and guaranteed that it will have the same result. This makes concurrent programming very easy. Threads are very cheap: it is possible to have 100,000 threads running at once.<ref>{{Cite web |url=http://www.mozart-oz.org/documentation/tutorial/node8.html#chapter.concurrency |title=Archived copy |access-date=29 November 2008 |archive-url=https://web.archive.org/web/20150224185115/http://www.mozart-oz.org/documentation/tutorial/node8.html#chapter.concurrency |archive-date=24 February 2015 |url-status=dead }}</ref>
Line 185:
===Example: Trial division sieve===
This example computes a stream of prime numbers using the [[trial division]] algorithm by recursively creating concurrent stream agents that filter out non-prime numbers:
<
fun {Sieve Xs}
case Xs of nil then nil
Line 193:
end
end
</syntaxhighlight>
=== Laziness ===
Line 207:
| doi=10.1145/72551.72554}}
</ref> is possible. Below, the fact is only computed when value of X is needed to compute the value of Y.
<
fun lazy {Fact N}
if N =< 0 then 1 else N*{Fact N-1} end
Line 215:
Y = X + 1
end
</syntaxhighlight>
[[lazy evaluation]] gives the possibility of storing truly infinite data structures in Oz. The power of lazy evaluation can be seen from the following code sample:
<
declare
fun lazy {Merge Xs Ys}
Line 240:
H = 1 | {Merge {Times 2 H} {Merge {Times 3 H} {Times 5 H}}}
{Browse {List.take H 6}}
</syntaxhighlight>
The code above elegantly computes all the [[Regular Number]]s<ref name="Hamming Numbers">
{{cite journal
Line 255:
=== Message passing concurrency ===
The declarative concurrent model can be extended with message passing via simple semantics:
<
declare
local Stream Port in
Line 264:
{Send Port n} % Stream is now 1|2| .. |n|_
end
</syntaxhighlight>
With a port and a thread, asynchronous agents can be defined:
<
fun {NewAgent Init Fun}
Msg Out in
Line 273:
{NewPort Msg}
end
</syntaxhighlight>
=== State and objects ===
It is again possible to extend the declarative model to support state and object-oriented programming with very simple semantics. To create a new mutable data structure called Cells:
<
local A X in
A = {NewCell 0}
Line 284:
X = @A % @ is used to access the value of A
end
</syntaxhighlight>
With these simple semantic changes, the whole object-oriented paradigm can be supported. With a little syntactic sugar, OOP becomes well integrated in Oz.
<
class Counter
attr val
Line 306:
{C browse}
end
</syntaxhighlight>
==Execution speed==
|