TLS Chapter #03 Examples in Oz
%%%%%%%%%%%%%%%%%%% The Little Schemer - Chapter - 3 %%%%%%%%%%%%%%%%%%%%%%
% 3.45
fun {Rember L A}
case L
of nil then nil
[] H|T then
if A == H
then T
else H|{Rember T A}
end
else fail
end
end
% 3.1
local A Lat in
A = mint
Lat = [lamb chops and mint jelly]
{Browse 1#{Rember Lat A}}
end
% 3.2
local A Lat in
A = mint
Lat = [lamb chops and mint flavored mint jelly]
{Browse 2#{Rember Lat A}}
end
% 3.3
local A Lat in
A = toast
Lat = [bacon lettuce and tomato]
{Browse 3#{Rember Lat A}}
end
% 3.4
local A Lat in
A = cup
Lat = [coffee cup tea cup and hick cup]
{Browse 4#{Rember Lat A}}
end
% 3.17
local A Lat in
A = bacon
Lat = [bacon lettuce and tomato]
{Browse 17#{Rember Lat A}}
end
% 3.27 and 3.45
local A Lat in
A = and
Lat = [bacon lettuce and tomato]
{Browse 27#{Rember Lat A}}
end
% 3.88
local A Lat in
A = sauce
Lat = [soy sauce and tomato sauce]
{Browse 88#{Rember Lat A}}
end
% 3.95
fun {Firsts L}
case L
of nil then nil
[] (H|_)|T then H|{Firsts T}
else fail
end
end
% 3.89
local L in
L = [[apple peach pumpkin]
[plum pear cherry]
[grape raisin pea]
[bean carrot eggplant]]
{Browse 89#{Firsts L}}
end
% 3.90
local L in
L = [[a b] [c d] [e f]]
{Browse 90#{Firsts L}}
end
% 3.91
local L in
L = nil
{Browse 91#{Firsts L}}
end
% 3.92
local L in
L = [[five plums]
[four]
[eleven green oranges]]
{Browse 92#{Firsts L}}
end
% 3.93
local L in
L = [[[five plums] four]
[eleven green oranges]
[[no] more]]
{Browse 93#{Firsts L}}
end
% 3.106
fun {Seconds L}
case L
of nil then nil
[] (_|B|_)|T then B|{Firsts T}
else fail
end
end
local L in
L = [[a b] [c d] [e f]]
{Browse 90#{Seconds L}}
end
% 3.138
fun {InsertR L New Old}
case L
of nil then nil
[] H|T then
if Old == H
then H|New|T
else H|{InsertR T New Old}
end
else fail
end
end
% 3.124
local New Old Lat in
New = topping
Old = fudge
Lat = [ice cream with fudge 'for' dessert]
{Browse 124#{InsertR Lat New Old}}
end
% 3.125
local New Old Lat in
New = jalapeņo
Old = and
Lat = [tacos tamales and salsa]
{Browse 125#{InsertR Lat New Old}}
end
% 3.126
local New Old Lat in
New = e
Old = d
Lat = [a b c d f g d h]
{Browse 126#{InsertR Lat New Old}}
end
% 3.135 and 3.139
local New Old Lat in
New = topping
Old = fudge
Lat = [ice cream with fudge 'for' dessert]
{Browse 135#{InsertR Lat New Old}}
end
% 3.144
fun {InsertL L New Old}
case L
of nil then nil
[] H|T then
if Old == H
then New|H|T
else H|{InsertL T New Old}
end
else fail
end
end
% 3.146
fun {Subst L New Old}
case L
of nil then nil
[] H|T then
if Old == H
then New|T
else H|{Subst T New Old}
end
else fail
end
end
local New Old Lat in
New = topping
Old = fudge
Lat = [ice cream with fudge 'for' dessert]
{Browse 146#{Subst Lat New Old}}
end
% 3.147
fun {Subst2 L New O1 O2}
case L
of nil then nil
[] H|T then
if O1 == H
then New|T
elseif O2 == H then New|T
else H|{Subst2 T New O1 O2}
end
else fail
end
end
local New O1 O2 Lat in
New = vanilla
O1 = chocolate
O2 = banana
Lat = [banana ice cream with chocolate topping]
{Browse 147#{Subst2 Lat New O1 O2}}
end
% 3.151
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
local A Lat in
A = cup
Lat = [coffee cup tea cup and hick cup]
{Browse 151#{MultiRember Lat A}}
end
% 3.182
fun {MultiInsertR L New Old}
case L
of nil then nil
[] H|T then
if Old == H
then H|New|{MultiInsertR T New Old}
else H|{MultiInsertR T New Old}
end
else fail
end
end
% 3.183
fun {MultiInsertL L New Old}
case L
of nil then nil
[] H|T then
if Old == H
then New|H|{MultiInsertL T New Old}
else H|{MultiInsertL T New Old}
end
else fail
end
end
% 3.187
fun {MultiSubst L New Old}
case L
of nil then nil
[] H|T then
if Old == H
then New|{MultiSubst T New Old}
else H|{MultiSubst T New Old}
end
else fail
end
end
|