TSS Chapter #11 Examples in Oz
% Defined in previous chapters
fun {IsZero X} X == 0 end
fun {Sub1 N} N - 1 end
%%%%%%%%%%%%%%%%%%% The Little Schemer - Chapter - 11 %%%%%%%%%%%%%%%%%%%%%%
% 11.6
fun {IsMember L A}
case L
of nil then false
[] H|T then
if A == H
then true
else {IsMember T A}
end
end
end
% 11.7
local A Lat in
A = sardines
Lat = ['Italian' sardines spaghetti parsley]
{Browse 7#{IsMember Lat A}}
end
% 11.15
fun {IsFirst L A}
case L
of nil then false
[] H|_ then H == A
end
end
% 11.16
fun {TwoInARow L}
case L
of nil then false
[] H|T then {IsFirst T H} orelse {TwoInARow T}
end
end
% 11.8
local Lat in
Lat = ['Italian' sardines spaghetti parsley]
{Browse 8#{TwoInARow Lat}}
end
% 11.10
local A Lat in
A = sardines
Lat = ['Italian' sardines sardines spaghetti parsley]
{Browse 10#{TwoInARow Lat}}
end
% 11.11
local Lat in
Lat = ['Italian' sardines more sardines spaghetti]
{Browse 11#{TwoInARow Lat}}
end
% 11.22
fun {TwoInARow_1 L}
case L
of nil then false
[] H|T then {IsFirstB T H}
end
end
fun {IsFirstB L A}
case L
of nil then false
[] H|T then H == A orelse {TwoInARow_1 L}
end
end
% 11.29
fun {TwoInARowB L Preceding}
case L
of nil then false
[] H|T then H == Preceding orelse {TwoInARowB T H}
end
end
% 11.33
fun {TwoInARow_2 L}
case L
of nil then false
[] H|T then {TwoInARowB T H}
end
end
% 11.35
local Lat in
Lat = [b d e i i a g]
{Browse 35#{TwoInARow_2 Lat}}
end
% 11.36
local Lat in
Lat = [d e i i a g]
{Browse 36#(nil == Lat)}
end
% 11.37
local Lat Preceding in
Preceding = b
Lat = [d e i i a g]
{Browse 37#(Lat.1 == Preceding)}
end
% 11.55
fun {SumOfPrefixesB Tup Sonssf}
case Tup
of nil then nil
[] H|T then (Sonssf + H) | {SumOfPrefixesB T Sonssf+H}
end
end
% 11.62
fun {SumOfPrefixes Tup}
{SumOfPrefixesB Tup 0}
end
% 11.44
local Tup in
Tup = [2 1 9 17 0]
{Browse 44#{SumOfPrefixes Tup}}
end
% 11.45
local Tup in
Tup = [1 1 1 1 1]
{Browse 45#{SumOfPrefixes Tup}}
end
% 11.77
% fun {Pick L N} {Nth L N} end
fun {Pick L N}
case L
of H|T then
if {IsZero {Sub1 N}}
then H
else {Pick T {Sub1 N}}
end
end
end
% 11.78
fun {ScrambleB Tup RevPre}
case Tup
of nil then nil
[] H|T then {Nth H|RevPre H} | {ScrambleB T H|RevPre}
end
end
% 11.84
fun {Scramble Tup}
{ScrambleB Tup nil}
end
% 11.66
local Tup in
Tup = [1 1 1 3 4 2 1 1 9 2]
{Browse 66#{Scramble Tup}}
end
% 11.67
local Tup in
Tup = [1 2 3 4 5 6 7 8 9]
{Browse 67#{Scramble Tup}}
end
% 11.68
local Tup in
Tup = [1 2 3 1 2 3 4 1 8 2 10]
{Browse 68#{Scramble Tup}}
end
% 11.75
local N Lat in
N = 4
Lat = [4 3 1 1 1]
{Browse 75#{Nth Lat N}}
end
% 11.76
local N Lat in
N = 2
Lat = [2 4 3 1 1 1]
{Browse 76#{Nth Lat N}}
end
|