TSS Chapter #17 Examples in Oz % Defined in previous chapters fun {Add1 N} N + 1 end fun {Sub1 N} N - 1 end fun {IsAtomS X} if {IsAtom X} andthen {Not X == nil} then true else {IsNumber X} end end 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 fun {Find N Ns Rs} fun {A Ns Rs} case Ns#Rs of (Hn|Tn)#(Hr|Tr) then if Hn == N then Hr else {A Tn Tr} end else notfound end end in {A Ns Rs} end %%%%%%%%%%%%%%%%%%% The Little Schemer - Chapter - 17 %%%%%%%%%%%%%%%%%%%%%% % 17.2 fun {Deep M} if M == 0 then pizza else {Deep {Sub1 M}}|nil end end % 17.3 local Rs = {NewCell nil} Ns = {NewCell nil} in fun {DeepM_1 N} fun {D M} if M == 0 then pizza else {D {Sub1 M}}|nil end end Exists = {Find N @Ns @Rs} in if Exists == notfound then local Result = {D N} in Rs := Result|@Rs Ns := N|@Ns Result end else Exists end end end local Rs = {NewCell nil} Ns = {NewCell nil} in fun {DeepM_2 N} fun {D M} if M == 0 then pizza else {DeepM_2 {Sub1 M}}|nil end end Exists = {Find N @Ns @Rs} in if Exists == notfound then local Result = {D N} in Rs := Result|@Rs Ns := N|@Ns Result end else Exists end end end % 17.5 local Rs = {NewCell nil} Ns = {NewCell nil} in fun {D M} if M == 0 then pizza else {DeepM_3 {Sub1 M}}|nil end end fun {DeepM_3 N} Exists = {Find N @Ns @Rs} in if Exists == notfound then local Result = {D N} in Rs := Result|@Rs Ns := N|@Ns Result end else Exists end end end % 17.7 local Rs = {NewCell nil} Ns = {NewCell nil} fun {D M} if M == 0 then pizza else {DeepM_4 {Sub1 M}}|nil end end in fun {DeepM_4 N} Exists = {Find N @Ns @Rs} in if Exists == notfound then local Result = {D N} in Rs := Result|@Rs Ns := N|@Ns Result end else Exists end end end % 17.8 local Rs = {NewCell nil} Ns = {NewCell nil} in fun {DeepM_5 N} Exists = {Find N @Ns @Rs} in if Exists == notfound then local Result = { fun {$ M} if M == 0 then pizza else {DeepM_5 {Sub1 M}}|nil end end N} in Rs := Result|@Rs Ns := N|@Ns Result end else Exists end end end % 17.8 local Rs = {NewCell nil} Ns = {NewCell nil} in fun {DeepM_6 N} Exists = {Find N @Ns @Rs} in if Exists == notfound then local Result = if N == 0 then pizza else {DeepM_6 {Sub1 N}}|nil end in Rs := Result|@Rs Ns := N|@Ns Result end else Exists end end end % 17.19 {Browse 19#{Deep 5}} % 17.20 {Browse 20#{Deep 3}} % 17.21 {Browse 21#{Deep 1000}} % 17.22 {For 1000 0 ~1 proc {$ N} _ = {Deep N} end} % 17.26 local N = {NewCell 0} in fun {ConsC_1 X Y} N := {Add1 @N} X|Y end end % 17.29 fun {Deep_1 M} if M == 0 then pizza else {ConsC_1 {Deep_1 {Sub1 M}} nil} end end % 17.31 {Browse 31#{Deep_1 5}} % 17.34 Counter local N = {NewCell 0} in Counter = fun {$} @N end fun {ConsC_2 X Y} N := {Add1 @N} X|Y end end fun {Deep_2 M} if M == 0 then pizza else {ConsC_2 {Deep_2 {Sub1 M}} nil} end end % 17.42 {Browse 42#{Deep_2 5}} % 17.43 {Browse 43#{Counter}} % 17.45 {Browse 45#{Deep_2 7}} % 17.46 {Browse 46#{Counter}} % 17.48 fun {SuperCounter F} fun {S N} if N == 0 then {F N} else _ = {F N} {S {Sub1 N}} end end in _ = {S 1000} {Counter} end % 17.49 {Browse 49#{SuperCounter Deep_2}} % 17.55 Counter3 SetCounter3 local N = {NewCell 0} in Counter3 = fun {$} @N end SetCounter3 = fun {$ X} N := X @N end fun {ConsC_3 X Y} N := {Add1 @N} X|Y end end fun {Deep_3 M} if M == 0 then pizza else {ConsC_3 {Deep_3 {Sub1 M}} nil} end end fun {SuperCounter3 F} fun {S N} if N == 0 then {F N} else _ = {F N} {S {Sub1 N}} end end in _ = {S 1000} {Counter3} end % 17.57 {Browse 57#{SetCounter3 0}} % 17.59 {Browse 59#{SuperCounter3 Deep_3}} % 17.62 local Rs = {NewCell nil} Ns = {NewCell nil} in proc {ResetMemo_7} Rs := nil Ns := nil end fun {DeepM_7 N} Exists = {Find N @Ns @Rs} in if Exists == notfound then local Result = if N == 0 then pizza else {ConsC_3 {DeepM_7 {Sub1 N}} nil} end in Rs := Result|@Rs Ns := N|@Ns Result end else Exists end end end % 17.63 {Browse 63#{DeepM_7 5}} % 17.64 {Browse 64#{Counter3}} % 17.66 {Browse 66#{SetCounter3 0}} {ResetMemo_7} % 17.67 {Browse 67#{DeepM_7 5}} % 17.68 {Browse 68#{Counter3}} % 17.69 {Browse 69#{DeepM_7 7}} % 17.70 {Browse 70#{Counter3}} % 17.74 {Browse 74#{SuperCounter3 DeepM_7}} % 17.78 fun {Rember1Star L A} fun {R L Oh} case L of nil then raise Oh(no) end [] H|T then if {IsAtomS H} then if H == A then T else H|{R T Oh} end else try {R H oh}|T catch oh(_) then H|{R T Oh} end end end end in try {R L say} catch say(_) then L end end fun {Rember1StarC L A} fun {R L Oh} case L of nil then raise Oh(no) end [] H|T then if {IsAtomS H} then if H == A then T else {ConsC_3 H {R T Oh}} end else try {R H oh}|T catch oh(_) then {ConsC_3 H {R T Oh}} end end end end in try {R L say} catch say(_) then L end end % 17.79 {Browse 79#{SetCounter3 0}} {ResetMemo_7} % 17.80 local A L in A = noodles L = [[food] more [food]] {Browse 80#{Rember1StarC L A}} end % 17.81 {Browse 81#{Counter3}} % 17.82 fun {Rember1Star2 L A} fun {R L} case L of nil then nil [] H|T then if {IsAtomS H} then if H == A then T else H|{R T} end else if {EqList H {R H}} then H|{R T} else {R H}|T end end end end in {R L} end fun {Rember1StarC2 L A} fun {R L} case L of nil then nil [] H|T then if {IsAtomS H} then if H == A then T else {ConsC_3 H {R T}} end else if {EqList H {R H}} then {ConsC_3 H {R T}} else {ConsC_3 {R H} T} end end end end in {R L} end % 17.83 {Browse 83#{SetCounter3 0}} {ResetMemo_7} % 17.84 local F M in F = food M = more {Browse {ConsC_3 {ConsC_3 F nil} {ConsC_3 M {ConsC_3 {ConsC_3 F nil} nil}}}} end % 17.85 {Browse 85#{Counter3}} % 17.86 {Browse 86#{SetCounter3 0}} {ResetMemo_7} % 17.87 local A L in A = noodles L = [[food] more [food]] {Browse 87#{Rember1StarC2 L A}} end % 17.88 {Browse 88#{Counter3}} |