First Oz example.
{Browse 5}
%% declaring a variable
declare
% N is an identifier (a name); must start with a capital letter
% 7 is its value
% the result of the declaration is a store variable N that has a value 7
N = 7
{Browse N * N}
%% function definition and recursion:
declare
fun {Fact N}
if N == 0 then 1 else N * {Fact N-1} end
end
{Browse {Fact 8}}
%% Lists:
declare
L = [3 6 7 8]
{Browse L}
% a list consists of two parts: head (a.k.a first) which is the first
% element, and tail (a.k.a rest), which is the rest of the list
% anything with two parts can be decomposed with .1 and .2 selectors:
{Browse L.1} % first element
{Browse L.2} % the rest of the list
% selectors can be nested:
{Browse L.2.1} % the first element of the rest of the list
{Browse L.2.2.2.2} % the rest of the rest of the rest of the rest of the list: nil
% You can also do pattern-matching on lists.
% Patttern-matching deconstructs a multi-part value (such as a list)
% and assigns its parts to variables:
case L of H|T then {Browse H} {Browse T} end
% Using pattern matching in functions
declare
fun {Second List}
case List of H|T then
if T==nil then nil else T.1 end
else nil
end
end
%%% Pattern-matching directly on nil
declare
fun {OneElement L}
case L of H|nil then 1
else 0
end
end
{Browse {OneElement [1]}}
{Browse {OneElement L}}
{Browse {OneElement nil}}
%% constructing lists:
declare
L1 = 4 | 5 | nil
{Browse L1}
%% Lazy evaluation
fun lazy {Ints N}
N | {Ints N + 1}
end
declare
I = {Ints 0}
{Browse I}
{Browse I.1}
{Browse I.2.1}
CSci 4651
course web site.