TLS Chapter #07 Examples in Oz
% Defined in previous chapters
fun {MultiRember L A}
case L
of nil then nil
[] H|T then
if A == H
then {MultiRember T A}
else H|{MultiRember T A}
end
else fail
end
end
%%%%%%%%%%%%%%%%%%% The Little Schemer - Chapter - 7 %%%%%%%%%%%%%%%%%%%%%%
% 7.4
fun {IsSet L}
case L
of nil then true
[] H|T then
if {Member H T}
then false
else {IsSet T}
end
end
end
% 7.1
{Browse 1#{IsSet [apple peaches apple plum]}}
% 7.2
local Lat in
Lat = [apples peaches pears plums]
{Browse 2#{IsSet Lat}}
end
% 7.3
local Lat in
Lat = nil
{Browse 3#{IsSet Lat}}
end
% 7.6
{Browse 6#{IsSet [apple 3 pear 4 9 apple 3 4]}}
% 7.9
fun {MakeSet0 L}
case L
of nil then nil
[] H|T then
if {Member H T}
then {MakeSet0 T}
else H|{MakeSet0 T}
end
end
end
% 7.11
local Lat in
Lat = [apple peach pear peach
plum apple lemon peach]
{Browse 11#{MakeSet0 Lat}}
end
% 7.12
fun {MakeSet L}
case L
of nil then nil
[] H|T then H | {MakeSet {MultiRember T H}}
end
end
% 7.13
local Lat in
Lat = [apple peach pear peach
plum apple lemon peach]
{Browse 13#{MakeSet Lat}}
end
% 7.15
{Browse 15#{MakeSet [apple 3 pear 4 9 apple 3 4]}}
% 7.18
fun {SubSet_ Set1 Set2}
case Set1
of nil then true
[] H|T then
if {Member H Set2}
then {SubSet_ T Set2}
else false
end
end
end
% 7.20
fun {SubSet Set1 Set2}
case Set1
of nil then true
[] H|T then {Member H Set2} andthen {SubSet T Set2}
end
end
% 7.16
local Set1 Set2 in
Set1 = [5 chicken wings]
Set2 = [5 hamburgers
2 pieces fried chicken and
light duckling wings]
{Browse 16#{SubSet Set1 Set2}}
end
% 7.17
local Set1 Set2 in
Set1 = [4 pounds 'of' horseradish]
Set2 = [4 pounds chicken and
2 pieces fried chicken and
5 ounces horseradish]
{Browse 17#{SubSet Set1 Set2}}
end
% 7.22 - 6.23
fun {EqSet Set1 Set2}
{SubSet Set1 Set2} andthen {SubSet Set2 Set1}
end
% 7.21
local Set1 Set2 in
Set1 = [6 large chickens with wings]
Set2 = [6 chickens with large wings]
{Browse 21#{EqSet Set1 Set2}}
end
% 7.26 and 6.27
fun {IsIntersect_ Set1 Set2}
case Set1
of nil then false
[] H|T then
if {Member H Set2}
then true
else {IsIntersect_ T Set2}
end
end
end
% 7.28
fun {IsIntersect Set1 Set2}
case Set1
of nil then false
[] H|T then {Member H Set2} orelse {IsIntersect T Set2}
end
end
% 7.25
local Set1 Set2 in
Set1 = [stewed tomatoes and macaroni]
Set2 = [macaroni and cheese]
{Browse 25#{IsIntersect Set1 Set2}}
end
% 7.30
fun {Intersect Set1 Set2}
case Set1
of nil then nil
[] H|T then
if {Member H Set2}
then H | {Intersect T Set2}
else {Intersect T Set2}
end
end
end
% 7.29
local Set1 Set2 in
Set1 = [stewed tomatoes and macaroni]
Set2 = [macaroni and cheese]
{Browse 29#{Intersect Set1 Set2}}
end
% 7.32
fun {Union Set1 Set2}
case Set1
of nil then Set2
[] H|T then
if {Member H Set2}
then {Union T Set2}
else H|{Union T Set2}
end
end
end
% 7.31
local Set1 Set2 in
Set1 = [stewed tomatoes and macaroni casserole]
Set2 = [macaroni and cheese]
{Browse 31#{Union Set1 Set2}}
end
% 7.33
fun {SetDifference Set1 Set2}
case Set1
of nil then nil
[] H|T then
if {Member H Set2}
then {SetDifference T Set2}
else H|{SetDifference T Set2}
end
end
end
% 7.36
fun {IntersectAll LSet}
case LSet
of H|nil then H
[] H|T then {Intersect H {IntersectAll T}}
end
end
% 7.34
local LSet in
LSet = [[a b c] [c a d e] [e f g h a b]]
{Browse 34#{IntersectAll LSet}}
end
% 7.35
local LSet in
LSet = [[6 pears and]
[3 peaches and 6 peppers]
[8 pears and 6 plums]
[and 6 prunes with some apples]]
{Browse 35#{IntersectAll LSet}}
end
% 7.41
fun {IsPair P}
case P
of _|_|nil then true
else false
end
end
% 7.37
{Browse 37#{IsPair [pear pear]}}
% 7.38
{Browse 38#{IsPair [3 7]}}
% 7.39
{Browse 39#{IsPair [[2] [pair]]}}
% 7.40
local L in
L = [full [house]]
{Browse 40#{IsPair L}}
end
% 7.47
fun {First P} P.1 end
fun {Second P} P.2.1 end
fun {Build S1 S2} S1|S2|nil end
% 7.48
fun {Third P} P.2.2.1 end
% not defined in book
fun {IsRel L}
case L
of nil then true
[] H|T then {IsSet L} andthen {IsPair H} andthen {IsRel T}
else false
end
end
fun {Firsts L}
case L
of nil then nil
[] [H _]|T then H | {Firsts T}
end
end
fun {Seconds L}
case L
of nil then nil
[] [_ H]|T then H | {Seconds T}
end
end
% 7.49
local L in
L = [apples peaches pumpkin pie]
{Browse 49#{IsRel L}}
end
% 7.50
local L in
L = [[apples peaches] [pumpkin pie] [apples peaches]]
{Browse 50#{IsRel L}}
end
% 7.51
local L in
L = [[apples peaches] [pumpkin pie]]
{Browse 51#{IsRel L}}
end
% 7.52
local L in
L = [[4 3] [4 2] [7 6] [6 2] [3 4]]
{Browse 52#{IsRel L}}
end
% 7.56
fun {IsFun Rel}
{IsSet {Firsts Rel}}
end
% 7.53
local Rel in
Rel = [[4 3] [4 2] [7 6] [6 2] [3 4]]
{Browse 53#{IsFun Rel}}
end
% 7.54
local Rel in
Rel = [[8 3] [4 2] [7 6] [6 2] [3 4]]
{Browse 54#{IsFun Rel}}
end
% 7.55
local Rel in
Rel = [[d 4] [b 0] [b 9] [e 5] [g 4]]
{Browse 55#{IsFun Rel}}
end
% 7.60 and 6.61
fun {RevRel Rel}
case Rel
of nil then nil
[] [X Y]|T then [Y X]| {RevRel T}
end
end
% 7.59
local Rel in
Rel = [[8 a] [pumpkin pie] [got sick]]
{Browse 59#{RevRel Rel}}
end
% 7.62
fun {RevPair Pair}
{Build {Second Pair} {First Pair}}
end
% 7.67
fun {IsFullFun Fun}
{IsSet {Seconds Fun}}
end
% 7.63
local Fun in
Fun = [[8 3] [4 2] [7 6] [6 2] [3 4]]
{Browse 63#{IsFullFun Fun}}
end
% 7.64
local Fun in
Fun = [[8 3] [4 8] [7 6] [6 2] [3 4]]
{Browse 64#{IsFullFun Fun}}
end
% 7.65
local Fun in
Fun = [[grape raisin]
[plum prune]
[stewed prune]]
{Browse 65#{IsFullFun Fun}}
end
% 7.66
local Fun in
Fun = [[grape raisin]
[plum prune]
[stewed grape]]
{Browse 66#{IsFullFun Fun}}
end
% 7.70
fun {OneToOne Fun}
{IsFun {RevRel Fun}}
end
% 7.71
{Browse 71#{OneToOne [[chocolate chip] [doughy cookie]]}}
|