Chapter #8 Examples in Oz % Defined in previous chapters fun {EqInt X Y} X == Y end %%%%%%%%%%%%%%%%%%% Chapter - 8 %%%%%%%%%%%%%%%%%%%%%% % 8.3 fun {EqOrangeOrApple X Y} case X#Y of orange#orange then true [] apple#apple then true [] _#_ then false end end % 8.4 fun {SubstituteOrangeOrApple N A L} case L of empty then empty [] cons(E T) then if {EqOrangeOrApple A E} then cons(N {SubstituteOrangeOrApple N A T}) else cons(E {SubstituteOrangeOrApple N A T}) end end end % 8.9 fun {Substitute Relation N A L} case L of empty then empty [] cons(E T) then if {Relation A E} then cons(N {Substitute Relation N A T}) else cons(E {Substitute Relation N A T}) end end end % 8.22 local L in L = cons(15 cons(6 cons(15 cons(17 cons(15 cons(8 empty)))))) {Browse 22#{Substitute EqInt 11 15 L}} end % 8.24 fun {LessThan X Y} X < Y end {Browse 24#{LessThan 15 17}} % 8.27 local L in L = cons(15 cons(6 cons(15 cons(17 cons(15 cons(8 empty)))))) {Browse 27#{Substitute LessThan 11 15 L}} end % 8.33 fun {InRange Small#Large X} if {LessThan Small X} then {LessThan X Large} else false end end % 8.29 {Browse 29#{InRange 11#16 15}} % 8.31 {Browse 31#{InRange 11#15 15}} {Browse 31#{InRange 15#22 15}} % 8.36 local L in L = cons(15 cons(6 cons(15 cons(17 cons(15 cons(8 empty)))))) {Browse 36#{Substitute InRange 22 11#16 L}} end % 8.40 fun {SubstituteUsingAPredicate Predicate N L} case L of empty then empty [] cons(E T) then if {Predicate E} then cons(N {SubstituteUsingAPredicate Predicate N T}) else cons(E {SubstituteUsingAPredicate Predicate N T}) end end end % 8.48 fun {Is15 N} {EqInt N 15} end % 8.49 local L in L = cons(15 cons(6 cons(15 cons(17 cons(15 cons(8 empty)))))) {Browse 49#{SubstituteUsingAPredicate Is15 11 L}} end % 8.52 fun {LessThan15 N} {LessThan N 15} end % 8.51 {Browse 51#{LessThan15 11}} % 8.55 local L in L = cons(15 cons(6 cons(15 cons(17 cons(15 cons(8 empty)))))) {Browse 55#{SubstituteUsingAPredicate LessThan15 11 L}} end % 8.59 fun {InRange_11_16 N} if {LessThan 11 N} then {LessThan N 16} else false end end % 8.57 {Browse 57#{InRange_11_16 15}} % 8.62 local L in L = cons(15 cons(6 cons(15 cons(17 cons(15 cons(8 empty)))))) {Browse 62#{SubstituteUsingAPredicate InRange_11_16 22 L}} end % 8.65 fun {InRangeC Small#Large} fun {$ X} if {LessThan Small X} then {LessThan X Large} else false end end end % 8.70 {Browse 70#{InRangeC 11#16}} % 8.71 fun {InRangeC_11_16 N} if {LessThan 11 N} then {LessThan N 16} else false end end % 8.76 local L in L = cons(15 cons(6 cons(15 cons(17 cons(15 cons(8 empty)))))) {Browse 76#{SubstituteUsingAPredicate {InRangeC 11#16} 22 L}} end % 8.77 local L in L = cons(15 cons(6 cons(15 cons(17 cons(15 cons(8 empty)))))) {Browse 77#{SubstituteUsingAPredicate {InRangeC 3#16} 22 L}} end % 8.79 fun {SubstituteC Predicate} fun {$ N L} case L of empty then empty [] cons(E T) then if {Predicate E} then cons(N {SubstituteUsingAPredicate Predicate N T}) else cons(E {SubstituteUsingAPredicate Predicate N T}) end end end end % 8.84 {Browse 84#{SubstituteC {InRangeC 11#16}}} % 8.85 {Browse 85#{SubstituteC InRangeC_11_16}} % 8.86 fun {SubstituteCInRange_11_16 N L} case L of empty then empty [] cons(E T) then if {InRange_11_16 E} then cons(N {SubstituteCInRange_11_16 N T}) else cons(E {SubstituteCInRange_11_16 N T}) end end end % 8.88 fun {Combine_ X Y} case X#Y of empty#empty then empty [] empty#cons(B L2) then cons(B L2) [] cons(A L1)#empty then cons(A L1) [] cons(A L1)#cons(B L2) then cons(A {Combine_ L1 cons(B L2)}) end end fun {Combine X Y} case X#Y of empty#L2 then L2 [] cons(A L1)#L2 then cons(A {Combine L1 L2}) end end % 8.90 local L1 L2 in L1 = cons(1 cons(2 cons(3 empty))) L2 = cons(5 cons(4 cons(7 cons(9 empty)))) {Browse 90#{Combine L1 L2}} end % 8.91 local L1 L2 in L1 = cons(1 cons(2 cons(3 empty))) L2 = cons(12 cons(11 cons(5 cons(5 empty)))) {Browse 91#{Combine L1 L2}} end % 8.93 fun {CombineC X} fun {$ Y} case X#Y of empty#L2 then L2 [] cons(A L1)#L2 then cons(A {Combine L1 L2}) end end end % 8.94 {Browse 94#{CombineC cons(1 cons(2 cons(3 empty)))}} % 8.95 fun {Prefixer_123 L2} cons(1 cons(2 cons(3 L2))) end % 8.97 fun {WaitingPrefixer_123 L2} cons(1 {{CombineC cons(2 cons(3 L2))} L2}) end |