TSS Chapter #16 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 Y = fun {$ Le} {fun {$ F} {F F} end fun {$ F} {Le fun {$ X} {{F F} X} end} end} end %%%%%%%%%%%%%%%%%%% The Little Schemer - Chapter - 16 %%%%%%%%%%%%%%%%%%%%%% % 16.1 fun {SweetTooth Food} Food|cake|nil end Last = {NewCell angelfood} % 16.2 local X in X = chocolate {Browse 2#{SweetTooth X}} end % 16.3 {Browse 3#@Last} % 16.4 local X in X = fruit {Browse 4#{SweetTooth X}} end % 16.5 {Browse 5#@Last} % 16.6 fun {SweetToothL Food} Last := Food Food|cake|nil end % 16.7 {Browse 7#{SweetToothL chocolate}} % 16.8 {Browse 8#@Last} % 16.9 {Browse 9#{SweetToothL fruit}} % 16.10 {Browse 10#@Last} % 16.12 local X in X = cheese {Browse 12#{SweetToothL X}} end % 16.9 {Browse 13#{SweetToothL carrot}} % 16.19 Ingredients = {NewCell nil} % 16.20 fun {SweetToothR Food} Ingredients := Food|@Ingredients Food|cake|nil end % 16.21 local X in X = chocolate {Browse 21#{SweetToothR X}} end % 16.22 {Browse 22#@Ingredients} % 16.23 {Browse 23#{SweetToothR fruit}} % 16.24 {Browse 24#@Ingredients} % 16.25 local X in X = cheese {Browse 25#{SweetToothR X}} end % 16.26 {Browse 26#@Ingredients} % 16.27 {Browse 27#{SweetToothR carrot}} % 16.28 {Browse 28#@Ingredients} % 16.35 fun {Deep M} if M == 0 then pizza else {Deep {Sub1 M}}|nil end end % 16.31 {Browse 31#{Deep 3}} % 16.32 {Browse 32#{Deep 7}} % 16.33 {Browse 33#{Deep 0}} % 16.39 Ns = {NewCell nil} fun {DeepR_0 N} Ns := N|@Ns {Deep N} end % 16.40 Rs = {NewCell nil} % Ns = {NewCell nil} fun {DeepR_1 N} Rs := {Deep N}|@Rs Ns := N|@Ns {Deep N} end % 16.42 fun {DeepR N} Result = {Deep N} in Rs := Result|@Rs Ns := N|@Ns Result end % 16.44 {Browse 44#{DeepR 3}} % 16.45 {Browse 45#@Ns} % 16.46 {Browse 46#@Rs} % 16.47 {Browse 47#{DeepR 5}} % 16.48 {Browse 48#@Ns} % 16.49 {Browse 49#@Rs} % 16.51 {Browse 51#{DeepR 3}} % 16.52 {Browse 52#@Ns} % 16.53 {Browse 53#@Rs} % 16.60 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 end end in {A Ns Rs} end % 16.57 {Browse 57#{Find 3 @Ns @Rs}} % 16.58 {Browse 58#{Find 5 @Ns @Rs}} % 16.59 try {Browse 59#{Find 7 @Ns @Rs}} catch _ then skip end % 16.62 fun {DeepM N} if {Member N @Ns} then {Find N @Ns @Rs} else {DeepR N} end end % 16.63 {Browse 63#@Ns} % 16.64 {Browse 64#@Rs} % 16.66 Ns := @Ns.2 Rs := @Rs.2 % 16.67 {Browse 67#@Ns} % 16.68 {Browse 68#@Rs} % 16.72 fun {DeepM_1 N} if {Member N @Ns} then {Find N @Ns @Rs} else local Result = {Deep N} in Rs := Result|@Rs Ns := N|@Ns Result end end end % 16.74 {Browse 74#{DeepM_1 6}} % 16.80 fun {Deep_2 M} if M == 0 then pizza else {DeepM_2 {Sub1 M}}|nil end end fun {DeepM_2 N} if {Member N @Ns} then {Find N @Ns @Rs} else local Result = {Deep_2 N} in Rs := Result|@Rs Ns := N|@Ns Result end end end % 16.81 {Browse 81#{DeepM_2 9}} % 16.82 {Browse 82#@Ns} % 16.88 local Rs = {NewCell nil} Ns = {NewCell nil} in fun {DeepM_3 N} if {Member N @Ns} then {Find N @Ns @Rs} else local Result = {Deep_3 N} in Rs := Result|@Rs Ns := N|@Ns Result end end end fun {Deep_3 M} if M == 0 then pizza else {DeepM_3 {Sub1 M}}|nil end end end % 16.89 {Browse 89#{DeepM_3 16}} % 16.91 try {Browse 91#{Find 3 nil nil}} catch _ then skip end % 16.93 fun {Find_1 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 % 16.96 local Rs = {NewCell nil} Ns = {NewCell nil} in fun {DeepM_4 N} if {Find_1 N @Ns @Rs} == notfound then local Result = {Deep_4 N} in Rs := Result|@Rs Ns := N|@Ns Result end else {Find_1 N @Ns @Rs} end end fun {Deep_4 M} if M == 0 then pizza else {DeepM_4 {Sub1 M}}|nil end end end local Rs = {NewCell nil} Ns = {NewCell nil} in fun {DeepM_5 N} Exists = {Find_1 N @Ns @Rs} in if Exists == notfound then local Result = {Deep_5 N} in Rs := Result|@Rs Ns := N|@Ns Result end else Exists end end fun {Deep_5 M} if M == 0 then pizza else {DeepM_5 {Sub1 M}}|nil end end end % 16.98 fun {LengthX L} case L of nil then 0 [] _|T then {Add1 {LengthX T}} end end % 16.99 LengthX_1 = {NewCell fun {$ L} 0 end} LengthX_1 := fun {$ L} case L of nil then 0 [] _|T then {Add1 {@LengthX_1 T}} end end fun {LengthX_2 L} case L of nil then 0 [] _|T then {Add1 {LengthX T}} end end {Browse 99#{(@LengthX_1) [1 2 3]}} {Browse 99#{LengthX_2 [1 2 3]}} % 16.100 fun {LengthX_3} H = {NewCell fun {$ L} 0 end} in H := fun {$ L} case L of nil then 0 [] _|T then {Add1 {{LengthX_3} T}} end end @H end {Browse 100#{{LengthX_3} [1 2 3]}} % 16#104 HX_4 = {NewCell fun {$ L} 0 end} fun {LengthX_4} HX_4 := fun {$ L} case L of nil then 0 [] _|T then {Add1 {{LengthX_4} T}} end end @HX_4 end {Browse 104#{{LengthX_4} [1 2 3]}} % 16.108 fun {L Length} fun {$ L} case L of nil then 0 [] _|T then {Add1 {Length T}} end end end {Browse 108#{{L Length} [1 2 3]}} % 16.109 fun {LengthX_5} H = {NewCell fun {$ Lx} 0 end} in H := fun {$ Lx} {{L fun {$ Arg} {@H Arg} end} Lx} end @H end {Browse 109#{{LengthX_5} [1 2 3]}} % 16.117 fun {Ybang L} H = {NewCell fun {$ Lx} nil end} in H := fun {$ Lx} {{L fun {$ Arg} {@H Arg} end} Lx} end @H end % 16.119 LengthX_6 = {Ybang L} {Browse 119#{LengthX_6 [1 2 3]}} % 16.120 fun {D Depth} fun {$ S} case S of nil then 1 [] H|T then if {IsAtomS H} then {Depth T} else {Add1 {Max {Depth H} {Depth T}}} end end end end Depth = {Ybang D} {Browse 120#{Depth [1 [[2]] 3]}} % 16.124 fun {Bizarre} X = {NewCell 0} in fun {$ F} X := {Add1 @X} fun {$ A} if A == @X then 0 else {F A} end end end end % 16.126 {Browse 126#{{Y {Bizarre}} 5}} % 16.127 {Browse 127#{{Ybang {Bizarre}} 5}} |