TLS Chapter #06 Examples in Oz
% Defined in previous chapters
fun {IsAtomS X}
if {IsAtom X} andthen {Not X == nil}
then true
else {IsNumber X}
end
end
%%%%%%%%%%%%%%%%%%% The Little Schemer - Chapter - 6 %%%%%%%%%%%%%%%%%%%%%%
% 6.1
{Browse 1#1}
% 6.2
{Browse 2#3}
% 6.3
{Browse 3#(1 + 3)}
% 6.4
{Browse 4#(1 + 3 * 4)}
% 6.5
{Browse 5#cookie}
% 6.6
local Y in
Y = 3
{Browse 6#{Pow 3 Y} + 5}
end
% 6.8
{Browse 8#'a'}
% 6.9
{Browse 9#'+'}
% 6.10
{Browse 10#'*'}
% 6.11
local Y in
Y = a
{Browse 11#('a' == Y)}
end
% 6.12
local X Y in
X = a
Y = a
{Browse 12#(X == Y)}
end
% 6.13
local N in
N = 3
{Browse 13#(N + 3)}
end
% 6.37
fun {IsNumbered AExp}
if {IsAtomS AExp}
then {IsNumber AExp}
else {IsNumbered AExp.1} andthen {IsNumbered AExp.2.2.1}
end
end
% 6.45 and 6.51
fun {ValueM NExp}
case NExp
of X|'+'|Y|nil then {ValueM X} + {ValueM Y}
[] X|'*'|Y|nil then {ValueM X} * {ValueM Y}
[] X|'^'|Y|nil then {Pow {ValueM X} {ValueM Y}}
else
if {IsNumber NExp}
then NExp
else raise domain("Malformed expression") end
end
end
end
% 6.39
local U in
U = 13
{Browse 39#{ValueM U}}
end
% 6.40
local X in
X = [1 '+' 3]
{Browse 40#{ValueM X}}
end
% 6.41
local Y in
Y = [1 '+' [3 '^' 4]]
{Browse 41#{ValueM Y}}
end
% 6.42
local Z in
Z = cookie
try
{Browse 42#{ValueM Z}}
catch _ then
skip
end
end
% 6.48
{Browse 48#{ValueM [1 '+' [3 '*' 4]]}}
% 6.57
fun {Value NExp}
case NExp
of '+'|X|Y|nil then {Value X} + {Value Y}
[] '*'|X|Y|nil then {Value X} * {Value Y}
[] '^'|X|Y|nil then {Pow {Value X} {Value Y}}
else
if {IsNumber NExp}
then NExp
else raise domain("Malformed expression") end
end
end
end
% 6.56
{Browse 56#{Value ['+' ['*' 3 6] ['^' 8 2]]}}
% 6.59
{Browse 59#{Value ['+' 1 3]}}
% 6.69
fun {FirstSubExp AExp}
case AExp
of _|X|_ then X
end
end
% 6.72
fun {SecondSubExp AExp}
case AExp
of _|_|Y|_ then Y
end
end
% 6.73
fun {Operator AExp}
case AExp
of H|_ then H
end
end
% 6.74
fun {Value_ NExp}
if {IsNumber NExp}
then NExp
else
case {Operator NExp}
of '+' then {Value_ {FirstSubExp NExp}} + {Value_ {SecondSubExp NExp}}
[] '*' then {Value_ {FirstSubExp NExp}} * {Value_ {SecondSubExp NExp}}
[] '^' then {Pow {Value_ {FirstSubExp NExp}} {Value_ {SecondSubExp NExp}}}
end
end
end
% 6.76
fun {FirstSubExp1 AExp}
case AExp
of X|_|_ then X
end
end
fun {Operator1 AExp}
case AExp
of _|H|_ then H
end
end
% 6.84
Zero = nil
% 6.85
One = [nil]
% 6.86
Two = [nil nil]
% 6.87
Three = [nil nil nil]
% 6.88
fun {IsSero L}
case L
of nil then true
else false
end
end
% 6.89
fun {Edd1 L}
nil|L
end
% 6.88
fun {Zub1 L}
case L
of H|T then T
end
end
% 6.92
local N in
N = nil
try
{Browse 92#{Zub1 N}}
catch _ then
skip
end
end
% 6.93
fun {Plus M N}
if {IsSero N}
then M
else {Edd1 {Plus M {Zub1 N}}}
end
end
% 6.95
fun {IsLat L}
if L == nil
then true
elseif {IsAtomS L.1} then
{IsLat L.2}
else false
end
end
% 6.96
local Ls in
Ls = [1 2 3]
{Browse 96#{IsLat Ls}}
end
% 6.98
local Ls in
Ls = [[nil] [nil nil] [nil nil nil]]
{Browse 97#{IsLat Ls}}
end
|