TLS Chapter #04 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 - 4 %%%%%%%%%%%%%%%%%%%%%%
% 4.1
{Browse 1#{IsAtomS 14}}
% 4.2
local N in
N = 14
{Browse 2#{IsAtomS N}}
end
% 4.3
{Browse 1#{IsNumber ~3}}
% 4.4
{Browse 4#{IsNumber 3.14159}}
% 4.5
% {Browse 5#({IsNumber ~3} andthen {IsNumber 3.14159})}
% 4.6
fun {Add1 N} N + 1 end
local N in
N = 67
{Browse 6#{Add1 N}}
end
% 4.7
{Browse 7#{Add1 67}}
% 4.8
fun {Sub1 N} N - 1 end
local N in
N = 5
{Browse 8#{Sub1 N}}
end
% 4.9
{Browse 9#{Sub1 0}}
% 4.10
fun {IsZero X} X == 0 end
{Browse 10#{IsZero 0}}
% 4.11
{Browse 10#{IsZero 1492}}
% 4.12
{Browse 12#(46 + 12)}
% 4.13
fun {Add M N}
if {IsZero N}
then M
else {Add {Add1 M} {Sub1 N}}
end
end
% 4.16
{Browse 16#(14 - 3)}
% 4.17
{Browse 17#(17 - 9)}
% 4.18
{Browse 18#(18 - 25)}
% 4.19
fun {Subtract M N}
if {IsZero N}
then M
else {Subtract {Sub1 M} {Sub1 N}}
end
end
% 4.21
{Browse 21#[2 11 3 79 47 6]}
% 4.22
{Browse 22#[8 55 5 555]}
% 4.23
{Browse 23#[1 2 8 apple 4 3]}
% 4.24
{Browse 24#[3 [7 4] 13 9]}
% 4.25
{Browse 25#nil}
% 4.26
fun {AddTup Tup}
case Tup
of nil then 0
[] H|T then
if {IsNumber H}
then {Add H {AddTup T}}
else fail
end
end
end
local Tup in
Tup = [3 5 2 8]
{Browse 26#{AddTup Tup}}
end
% 4.27
local Tup in
Tup = [15 6 7 12 3]
{Browse 27#{AddTup Tup}}
end
% 4.53
{Browse 53#(5 * 3)}
% 4.54
{Browse 54#(13 * 4)}
% 4.60
fun {Times M N}
if {IsZero N}
then 0
else {Add M {Times M {Sub1 N}}}
end
end
% 4.61
{Browse 61#{Times 12 3}}
% 4.77
fun {TupPlus Tup1 Tup2}
case Tup1#Tup2
of nil#_ then Tup2
[] _#nil then Tup1
[] (H1|T1)#(H2|T2) then {Add H1 H2}|{TupPlus T1 T2}
else fail
end
end
local Tup1 Tup2 in
Tup1 = [3 6 9 11 4]
Tup2 = [8 5 2 0 7]
{Browse 77#{TupPlus Tup1 Tup2}}
end
% 4.78
local Tup1 Tup2 in
Tup1 = [2 3]
Tup2 = [4 6]
{Browse 78#{TupPlus Tup1 Tup2}}
end
% 4.89
local Tup1 Tup2 in
Tup1 = [3 7]
Tup2 = [4 6]
{Browse 89#{TupPlus Tup1 Tup2}}
end
% 4.98
local Tup1 Tup2 in
Tup1 = [3 7]
Tup2 = [4 6 8 1]
{Browse 98#{TupPlus Tup1 Tup2}}
end
% 4.101
local Tup1 Tup2 in
Tup1 = [3 7 8 1]
Tup2 = [4 6]
{Browse 101#{TupPlus Tup1 Tup2}}
end
% 4.107
{Browse 107#(12 > 133)}
% 4.108
{Browse 108#(120 > 11)}
% 4.113
fun {GT X Y}
if {IsZero X}
then false
elseif {IsZero Y} then true
else {GT {Sub1 X} {Sub1 Y}}
end
end
% 4.114
{Browse 114#{GT 3 3}}
% 4.130
{Browse 130#(4 < 6)}
% 4.131
{Browse 131#(8 < 3)}
% 4.132
{Browse 132#(6 < 6)}
% 4.133
fun {LT X Y}
if {IsZero Y}
then false
elseif {IsZero X} then true
else {LT {Sub1 X} {Sub1 Y}}
end
end
% 4.134
fun {EQ X Y}
if {IsZero X}
then
if {IsZero Y}
then true
else false
end
else {EQ {Sub1 X} {Sub1 Y}}
end
end
fun {EQ2 X Y}
if {GT X Y}
then false
elseif {LT X Y} then false
else true
end
end
{Browse 134#{EQ2 3 3}}
% 4.136
{Browse 136#{Pow 1 1}}
% 4.137
{Browse 137#{Pow 2 3}}
% 4.138
{Browse 138#{Pow 5 3}}
% 4.139
fun {Power M N}
if {IsZero N}
then 1
else {Times M {Power M {Sub1 N}}}
end
end
% 4.140
fun {Divide M N}
if {LT M N}
then 0
else {Browse b#M#N}{Add1 {Divide {Subtract M N} N}}
end
end
% 4.145
{Browse 145#(15 div 4)}
% 4.147
local Lat in
Lat = [hotdogs with mustard sauerkraut and pickles]
{Browse 147#{Length Lat}}
end
% 4.148
local Lat in
Lat = [ham and cheese on rye]
{Browse 148#{Length Lat}}
end
% 4.149
fun {LengthX L}
case L
of nil then 0
[] _|T then {Add1 {LengthX T}}
end
end
% 4.150
local Lat in
Lat = [lasagna spaghetti ravioli macaroni meatball]
{Browse 150#{Nth Lat 4}}
end
% 4.151
local Lat in
Lat = [a]
try
{Browse 151#{Nth Lat 0}}
catch _ then
skip
end
end
% 4.152
fun {Pick L N}
case L
of H|T then
if {IsZero {Sub1 N}}
then H
else {Pick T {Sub1 N}}
end
else fail
end
end
% 4.154
fun {RemPick L N}
case L
of H|T then
if {IsZero {Sub1 N}}
then T
else H|{RemPick T {Sub1 N}}
end
[] nil then nil
else fail
end
end
% 4.153
local Lat in
Lat = [hotdogs with hot mustard]
{Browse 153#{RemPick Lat 3}}
end
% 4.155
{Browse 155#{IsNumber a}}
% 4.156
{Browse 156#{IsNumber 76}}
% 4.158
fun {NoNums L}
case L
of nil then nil
[] H|T then
if {IsNumber H}
then {NoNums T}
else H|{NoNums T}
end
else fail
end
end
% 4.159
fun {AllNums L}
case L
of nil then nil
[] H|T then
if {IsNumber H}
then H|{AllNums T}
else {AllNums T}
end
else fail
end
end
% 4.160
fun {Eqan A1 A2}
if {IsNumber A1} andthen {IsNumber A2}
then A1 == A2
elseif {IsNumber A1} orelse {IsNumber A2} then false
else A1 == A2
end
end
% 4.162
fun {Occur L A}
case L
of nil then 0
[] H|T then
if A == H
then {Add1 {Occur T A}}
else {Occur T A}
end
else fail
end
end
% 4.163
fun {IsOne N}
if {IsZero N}
then false
else {IsZero {Sub1 N}}
end
end
% 4.164
fun {IsOneX N}
N == 1
end
% 4.165
fun {RemPickX L N}
case L
of H|T then
if {IsOne N}
then T
else H|{RemPickX T {Sub1 N}}
end
[] nil then nil
else fail
end
end
|