Chapter #10 Examples in Oz % Defined in previous chapters fun {EqInt X Y} X == Y end %%%%%%%%%%%%%%%%%%% Chapter - 10 %%%%%%%%%%%%%%%%%%%%%% % 10.4 fun {PlusI N M} if {IsZeroI N} then M else {SuccI {PlusI {PredI N} M}} end end fun {IsZeroI N} {EqInt N 0} end fun {PredI N} if {EqInt N 0} then raise too_small end else N - 1 end end fun {SuccI N} N + 1 end % 10.1 {Browse 1#{PlusI 0 1}} % 10.2 {Browse 2#{PlusI 1 1}} % 10.3 {Browse 3#{PlusI 2 1}} % 10.6 fun {Plus N M} if {IsZero N} then M else {Succ {Plus {Pred N} M}} end end fun {IsZero N} case N of zero then true else false end end fun {Pred N} case N of zero then raise too_small end [] one_more_than(X) then X end end fun {Succ N} one_more_than(N) end % 10.10 local X Y in X = one_more_than(one_more_than(zero)) Y = one_more_than(one_more_than(one_more_than(zero))) {Browse 10#{Plus X Y}} end % 10.26 NUMBER_AS_NUM = functor export succ : Succ pred : Pred is_zero : IsZero define fun {IsZero N} case N of zero then true else false end end fun {Pred N} case N of zero then raise too_small end [] one_more_than(X) then X end end fun {Succ N} one_more_than(N) end end NUMBER_AS_INT = functor export succ : Succ pred : Pred is_zero : IsZero define fun {EqInt X Y} X == Y end fun {IsZero N} {EqInt N 0} end fun {Pred N} if {EqInt N 0} then raise too_small end else N - 1 end end fun {Succ N} N + 1 end end % 10.33 [IntStruct] = {Module.apply [NUMBER_AS_INT]} % 10.35 [NumStruct] = {Module.apply [NUMBER_AS_NUM]} % 10.41 PON = functor export init : Init plus : Plus define A_N proc {Init Mod} A_N = Mod end fun {Plus N M} if {A_N.is_zero N} then M else {A_N.succ {Plus {A_N.pred N} M}} end end end % 10.47 [IntArith] = {Module.apply [PON]} {IntArith.init IntStruct} % 10.51 [NumArith] = {Module.apply [PON]} {NumArith.init NumStruct} % 10.52 {Browse 47#{IntArith.plus 1 2}} % 10.58 {Browse 58#{NumArith.plus one_more_than(zero) one_more_than(one_more_than(zero))}} % 10.62 - 10.147 To Be Done |