TSS Chapter #13 Examples in Oz
%%%%%%%%%%%%%%%%%%% The Little Schemer - Chapter - 13 %%%%%%%%%%%%%%%%%%%%%%
% 13.3
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
% 13.1
local Set1 Set2 in
Set1 = [tomatoes and macaroni]
Set2 = [macaroni and cheese]
{Browse 1#{Intersect Set1 Set2}}
end
% 13.4
fun {Intersect_1 Set1 Set2}
fun {I Set}
case Set
of nil then nil
[] H|T then
if {Member H Set2}
then H | {I T}
else {I T}
end
end
end
in
{I Set1}
end
% 13.5
fun {IntersectAll LSet}
case LSet
of H|nil then H
[] H|T then {Intersect H {IntersectAll T}}
end
end
% 13.6
fun {IntersectAll_1 LSet}
case LSet
of nil then nil
[] H|nil then H
[] H|T then {Intersect H {IntersectAll_1 T}}
end
end
% 13.14
fun {IntersectAll_2 LSet}
fun {IntersectAll_2 LSet}
case LSet
of H|nil then H
[] H|T then {Intersect H {IntersectAll_2 T}}
end
end
in
case LSet
of nil then nil
else {IntersectAll_2 LSet}
end
end
fun {IntersectAll_3 LSet}
fun {A LSet}
case LSet
of H|nil then H
[] H|T then {Intersect H {A T}}
end
end
in
case LSet
of nil then nil
else {A LSet}
end
end
% 13.18
local LSet in
LSet = [[3 mangos and]
[3 kiwis and]
[3 hamburgers]]
{Browse 18#{IntersectAll_3 LSet}}
end
% 13.19
local LSet in
LSet = [[3 steaks and]
[no food and]
[three baked potatoes]
[3 diet hamburgers]]
{Browse 19#{IntersectAll_3 LSet}}
end
% 13.20
local LSet in
LSet = [[3 mangos and]
nil
[3 diet hamburgers]]
{Browse 19#{IntersectAll_3 LSet}}
end
% 13.28
%% Oz does not have letcc - emulate with try-catch???
fun {IntersectAll_5 LSet}
fun {Hop LSet}
fun {A LSet}
case LSet
of nil|_ then raise hop(nil) end
[] H|nil then H
[] H|T then {Intersect_1 H {A T}}
end
end
in
try
{A LSet}
catch hop(E) then
E
end
end
in
case LSet
of nil then nil
else {Hop LSet}
end
end
% 13.34
local LSet in
LSet = [[3 mangos and]
nil
[3 diet hamburgers]]
{Browse 34#{IntersectAll_5 LSet}}
end
% 13.58
local LSet in
LSet = [[3 steaks and]
[no food and]
[three baked potatoes]
[3 diet hamburgers]]
{Browse 58#{IntersectAll_5 LSet}}
end
% 13.66
local Set1 Set2 in
Set1 = [three baked potatoes]
Set2 = [3 diet hamburgers]
{Browse 66#{Intersect_1 Set1 Set2}}
end
% 13.68
local Set1 Set2 in
Set1 = [no food an]
Set2 = nil
{Browse 68#{Intersect_1 Set1 Set2}}
end
% 13.80
fun {Intersect_2 Set1 Set2}
fun {I Set1}
case Set1
of nil then nil
[] H|T then
if {Member H Set2}
then H | {I T}
else {I T}
end
end
end
in
case Set2
of nil then nil
else {I Set1}
end
end
% 13.90
fun {IntersectAll_6 LSet}
fun {Hop LSet}
fun {A LSet}
case LSet
of nil|_ then raise hop(nil) end
[] H|nil then H
[] H|T then {I H {A T}}
end
end
fun {I S1 S2}
fun {J S1}
case S1
of nil then nil
[] H|T then
if {Member H S2}
then H | {J T}
else {J T}
end
end
end
in
case S2
of nil then nil
else {J S1}
end
end
in
try
{A LSet}
catch hop(E) then
E
end
end
in
case LSet
of nil then nil
else {Hop LSet}
end
end
% 13.93
fun {IntersectAll_7 LSet}
fun {Hop LSet}
fun {A LSet}
case LSet
of nil|_ then raise hop(nil) end
[] H|nil then H
[] H|T then {I H {A T}}
end
end
fun {I S1 S2}
fun {J S1}
case S1
of nil then nil
[] H|T then
if {Member H S2}
then H | {J T}
else {J T}
end
end
end
in
case S2
of nil then fail
else {J S1}
end
end
in
try
{A LSet}
catch hop(E) then
E
end
end
in
case LSet
of nil then nil
else {Hop LSet}
end
end
% 13.94
local LSet in
LSet = [[3 steaks and]
[no food and]
[three baked potatoes]
[3 diet hamburgers]]
{Browse 93#{IntersectAll_6 LSet}}
end
% 13.112
fun {Rember L A}
fun {R L}
case L
of nil then nil
[] H|T then
if A == H
then T
else H|{R T}
end
end
end
in
{R L}
end
% 13.118
fun {RemberBeyondFirst L A}
fun {R L}
case L
of nil then nil
[] H|T then
if H == A
then nil
else H|{R T}
end
end
end
in
{R L}
end
% 13.113
local A Lat in
A = roots
Lat = [noodles
spaghetti spätzle 'bean-thread'
roots
potatoes yam
others
rice]
{Browse 113#{RemberBeyondFirst Lat A}}
end
% 13.114
local Lat in
Lat = [noodles
spaghetti spätzle 'bean-thread'
roots
potatoes yam
others
rice]
{Browse 114#{RemberBeyondFirst Lat others}}
end
% 13.115
local A Lat in
A = sweetthing
Lat = [noodles
spaghetti spätzle 'bean-thread'
roots
potatoes yam
others
rice]
{Browse 115#{RemberBeyondFirst Lat A}}
end
% 13.116
local Lat in
Lat = [cookies
chocolate mints
caramel delight ginger snapes
desserts
chocolate mousse
vanilla ice cream
'German' chocolate cake
more desserts
gingerbread chocalate
chip brownies]
{Browse 116#{RemberBeyondFirst Lat desserts}}
end
% 13.135
fun {RemberUptoLast L A}
fun {Skip L}
fun {R L}
case L
of nil then nil
[] H|T then
if H == A
then raise 'skip'(T) end
else H|{R T}
end
end
end
in
try
{R L}
catch 'skip'(E) then
{Skip E}
end
end
in
{Skip L}
end
% 13.119
local A Lat in
A = roots
Lat = [noodles
spaghetti spätzle 'bean-thread'
roots
potatoes yam
others
rice]
{Browse 119#{RemberUptoLast Lat A}}
end
% 13.120
local A Lat in
A = sweetthing
Lat = [noodles
spaghetti spätzle 'bean-thread'
roots
potatoes yam
others
rice]
{Browse 120#{RemberUptoLast Lat A}}
end
% 13.121
local A Lat in
A = cookies
Lat = [cookies
chocolate mints
caramel delight ginger snapes
desserts
chocolate mousse
vanilla ice cream
'German' chocolate cake
more cookies
gingerbread chocalate
chip brownies]
{Browse 121#{RemberUptoLast Lat A}}
end
|