TLS Chapter #05 Examples in Oz
% Defined in previous chapters
fun {IsAtomS X}
if {IsAtom X} andthen {Not X == nil}
then true
else {IsNumber X}
end
end
fun {IsLat L}
if L == nil
then true
elseif {IsAtomS L.1} then
{IsLat L.2}
else false
end
end
%%%%%%%%%%%%%%%%%%% The Little Schemer - Chapter - 5 %%%%%%%%%%%%%%%%%%%%%%
% 5.3
fun {RemberStar L A}
case L
of nil then nil
[] H|T then
if A == H
then {RemberStar T A}
else {RemberStar H A} | {RemberStar T A}
end
else L
end
end
% 5.1
local A L in
A = cup
L = [[coffee] cup [[tea] cup] [and [hick]] cup]
{Browse 1#{RemberStar L A}}
end
% 5.2
local A L in
A = sauce
L = [[[tomato sauce]] [[bean] sauce] [and [[flying]] sauce]]
{Browse 3#{RemberStar L A}}
end
% 5.4
local L in
L = [[[tomato sauce]] [[bean] sauce] [and [[flying]] sauce]]
{Browse 4#{IsLat L}}
end
% 5.5
local L in
L = [[[tomato sauce]] [[bean] sauce] [and [[flying]] sauce]]
{Browse 5#{IsAtomS L.1}}
end
% 5.7
fun {InsertRStar L New Old}
case L
of nil then nil
[] H|T then
if Old == H
then H|New|{InsertRStar T New Old}
else {InsertRStar H New Old}|{InsertRStar T New Old}
end
else L
end
end
% 5.6
local New Old L in
New = roast
Old = chuck
L = [[how much [wood]]
could
[[a [wood] chuck]]
[[[chuck]]]
['if' [a] [[wood chuck]]]
could chuck wood]
{Browse 6#{InsertRStar L New Old}}
end
% 5.18
fun {OccurStar L A}
case L
of nil then 0
[] H|T then
if A == H
then 1 + {OccurStar T A}
else {OccurStar H A} + {OccurStar T A}
end
else 0
end
end
% 5.17
local A L in
A = banana
L = [[banana]
[split [[[[banana ice]]] [cream [banana]] sherbet]]
[banana]
[bread]
[banana brandy]]
{Browse 17#{OccurStar L A}}
end
% 5.20
fun {SubstStar L New Old}
case L
of nil then nil
[] H|T then
if Old == H
then New|{SubstStar T New Old}
else {SubstStar H New Old}|{SubstStar T New Old}
end
else L
end
end
% 5.19
local New Old L in
New = orange
Old = banana
L = [[banana]
[split [[[[banana ice]]] [cream [banana]] sherbet]]
[banana]
[bread]
[banana brandy]]
{Browse 19#{SubstStar L New Old}}
end
% 5.22
fun {InsertLStar L New Old}
case L
of nil then nil
[] H|T then
if Old == H
then H|New|{InsertLStar T New Old}
else {InsertLStar H New Old}|{InsertLStar T New Old}
end
else L
end
end
% 5.21
local New Old L in
New = pecker
Old = chuck
L = [[how much [wood]]
could
[[a [wood] chuck]]
[[[chuck]]]
['if' [a] [[wood chuck]]]
could chuck wood]
{Browse 21#{InsertLStar L New Old}}
end
% 5.24
fun {IsMemberStar L A}
case L
of nil then false
[] H|T then
if A == H
then true
else {IsMemberStar H A} orelse {IsMemberStar T A}
end
else false
end
end
% 5.23
local A L in
A = chips
L = [[potato] [chips [[with] fish] [chips]]]
{Browse 23#{IsMemberStar L A}}
end
% 5.33
fun {LeftMost L}
case L
of nil then nil
[] H|_ then
if {IsAtomS H}
then H
else {LeftMost H}
end
else L
end
end
% 5.26
local L in
L = [[potato] [chips [[with] fish] [chips]]]
{Browse 26#{LeftMost L}}
end
% 5.27
local L in
L = [[[hot] [tuna [and]]] cheese]
{Browse 27#{LeftMost L}}
end
% 5.28
local L in
L = [[[nil four]] 17 [seventeen]]
{Browse 28#{LeftMost L}}
end
% 5.29
local L in
L = nil
{Browse 28#{LeftMost L}}
end
% 5.35
local X L in
X = pizza
L = [mozzarella pizza]
{Browse 35#({IsAtomS L.1} andthen (L.1 == X))}
end
% 5.37
local X L in
X = pizza
L = [[mozzarella mushroom] pizza]
{Browse 37#({IsAtomS L.1} andthen (L.1 == X))}
end
% 5.39
local X L in
X = pizza
L = [pizza [mozzarella mushroom]]
{Browse 39#({IsAtomS L.1} andthen (L.1 == X))}
end
% 5.55
fun {EqList L1 L2}
case L1#L2
of nil#nil then true
[] nil#_ then false
[] _#nil then false
[] (H1|T1)#(H2|T2) then
if {IsAtomS H1} andthen {IsAtomS H2}
then H1 == H2 andthen {EqList T1 T2}
else {EqList H1 H2} andthen {EqList T1 T2}
end
else false
end
end
% 5.42
local L1 L2 in
L1 = [strawberry ice cream]
L2 = [strawberry ice cream]
{Browse 42#{EqList L1 L2}}
end
% 5.43
local L1 L2 in
L1 = [strawberry cream ice]
L2 = [strawberry ice cream]
{Browse 43#{EqList L1 L2}}
end
% 5.44
local L1 L2 in
L1 = [banana [[split]]]
L2 = [banana [split]]
{Browse 44#{EqList L1 L2}}
end
% 5.45
local L1 L2 in
L1 = [beef [[sausage]] [and [soda]]]
L2 = [beef [[salami]] [and [soda]]]
{Browse 45#{EqList L1 L2}}
end
% 5.46
local L1 L2 in
L1 = [beef [[sausage]] [and [soda]]]
L2 = [beef [[sausage]] [and [soda]]]
{Browse 46#{EqList L1 L2}}
end
% 5.62
fun {Equal S1 S2}
if {IsAtomS S1} andthen {IsAtomS S2}
then S1 == S2
elseif {IsAtomS S1} then false
elseif {IsAtomS S2} then false
else {EqList S1 S2}
end
end
% 5.64
fun {Equal0 S1 S2}
if {IsAtomS S1} andthen {IsAtomS S2}
then S1 == S2
elseif {IsAtomS S1} then false
elseif {IsAtomS S2} then false
else {EqList0 S1 S2}
end
end
fun {EqList0 L1 L2}
case L1#L2
of nil#nil then true
[] nil#_ then false
[] _#nil then false
[] (H1|T1)#(H2|T2) then {Equal0 H1 H2} andthen {EqList0 T1 T2}
else false
end
end
% 5.66
fun {Rember L A}
case L
of nil then nil
[] H|T then
if {Equal A H}
then T
else H|{Rember T A}
end
else fail
end
end
|