Chapter #6 Examples in Oz
%%%%%%%%%%%%%%%%%%% Chapter - 6 %%%%%%%%%%%%%%%%%%%%%%
% 6.11
fun {FlatOnly T}
case T
of bud then true
[] flat(F X) then {FlatOnly X}
[] split(S X) then false
end
end
% 6.1
{Browse 1#{FlatOnly flat(apple flat(peach bud))}}
% 6.2
{Browse 2#{FlatOnly flat(pear bud)}}
% 6.3
{Browse 3#{FlatOnly split(
bud
flat(
fig
split(
bud
bud)))}}
% 6.4
{Browse 4#{FlatOnly split(
split(
bud
flat(
lemon
bud))
flat(
fig
split(
bud
bud)))}}
% 6.14
fun {SplitOnly T}
case T
of bud then true
[] flat(F X) then false
[] split(S X) then
if {SplitOnly S}
then {SplitOnly X}
else false
end
end
end
% 6.20
{Browse 20#{SplitOnly split(
split(
bud
split(
bud
bud))
split(
bud
split(
bud
bud)))}}
% 6.22
fun {ContainsFruit T}
case T
of bud then false
[] flat(F X) then true
[] split(S X) then
if {ContainsFruit S}
then {ContainsFruit X}
else false
end
end
end
fun {ContainsFruit_ T}
if {SplitOnly T}
then false
else true
end
end
% 6.21
{Browse 21#{ContainsFruit split(
split(
bud
split(
bud
bud))
split(
bud
split(
bud
bud)))}}
% 6.34
% Same as Max
fun {LargerOf N M}
if N > M
then N
else M
end
end
% 6.35
fun {Height T}
case T
of bud then 0
[] flat(F X) then 1 + {Height X}
[] split(S X) then 1 + {Max {Height S} {Height X}}
end
end
% 6.23
{Browse 23#{Height split(
split(
bud
flat(
lemon
bud))
flat(
fig
split(
bud
bud)))}}
% 6.24
{Browse 24#{Height split(
bud
flat(
lemon
bud))}}
% 6.25
{Browse 25#{Height flat(lemon bud)}}
% 6.26
{Browse 26#{Height bud}}
% 6.29
{Browse 29#{Height flat(
fig
flat(
lemon
flat(
apple
bud)))}}
% 6.30
{Browse 30#{Height split(
split(
bud
bud)
flat(
fig
flat(
lemon
flat(
apple
bud))))}}
% 6.36
{Browse 36#{Height split(bud bud)}}
% 6.39
fun {EqFruit X Y}
case X#Y
of peach#peach then true
[] apple#apple then true
[] pear#pear then true
[] lemon#lemon then true
[] fig#fig then true
[] _#_ then false
end
end
% 6.41
fun {SubstituteInTree N A T}
case T
of bud then bud
[] flat(F X) then
if {EqFruit F A}
then flat(N {SubstituteInTree N A X})
else flat(F {SubstituteInTree N A X})
end
[] split(S X) then
split(
{SubstituteInTree N A S}
{SubstituteInTree N A X})
end
end
% 6.38
{Browse 38#{SubstituteInTree
apple
fig
split(
split(
flat(
fig
bud)
flat(
fig
bud))
flat(
fig
flat(
lemon
flat(
apple
bud))))}}
% 6.43
fun {Occurs A T}
case T
of bud then 0
[] flat(F X) then
if {EqFruit F A}
then 1 + {Occurs A X}
else {Occurs A X}
end
[] split(S X) then
{Occurs A S} + {Occurs A X}
end
end
% 6.42
{Browse 42#{Occurs fig split(
split(
flat(
fig
bud)
flat(
fig
bud))
flat(
fig
flat(
lemon
flat(
apple
bud))))}}
% 6.45
{Browse 45#atom(5)}
% 6.46
{Browse 46#atom(fig)}
% 6.47
{Browse 47#slist(empty)}
% 6.49
{Browse 49#cons(atom(5) cons(atom(13) cons(atom(1) empty)))}
% 6.50
{Browse 50#cons(atom(fig) empty)}
% 6.57
fun {OccursInSlist A L}
case L
of empty then 0
[] cons(X Y) then {OccursInSexp A X} + {OccursInSlist A Y}
end
end
fun {OccursInSexp A S}
case S
of atom(X) then
if X == A
then 1
else 0
end
[] slist(X) then {OccursInSlist A X}
end
end
% 6.54
{Browse 54#{OccursInSlist fig cons(
atom(fig)
cons(
atom(fig)
cons(
atom(lemon)
empty)))}}
% 6.55
{Browse 55#{OccursInSlist fig cons(
slist(
cons(
atom(fig)
cons(
atom(peach)
empty)))
cons(
atom(fig)
cons(
atom(lemon)
empty)))}}
% 6.56
{Browse 56#{OccursInSexp fig slist(
cons(
atom(fig)
cons(
atom(peach)
empty)))}}
% 6.64
fun {RemoveFromSlist A L}
case L
of empty then empty
[] cons(atom(B) Y) then
if A == B
then {RemoveFromSlist A Y}
else cons(atom(B) {RemoveFromSlist A Y})
end
[] cons(slist(X) Y) then
cons(
slist({RemoveFromSlist A X})
{RemoveFromSlist A Y})
end
end
% 6.61
{Browse 61#{RemoveFromSlist fig cons(atom(fig) empty)}}
|