TSS Chapter #14 Examples in Oz % Defined in previous chapters 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 {Add1 N} N + 1 end %%%%%%%%%%%%%%%%%%% The Little Schemer - Chapter - 14 %%%%%%%%%%%%%%%%%%%%%% % 14.2 fun {LeftMost L} case L of H|_ then if {IsAtomS H} then H else {LeftMost H} end end end % 14.3 local L in L = [[[a] b] [c d]] {Browse 3#{LeftMost L}} end % 14.4 local L in L = [[[a] nil] nil [e]] {Browse 4#{LeftMost L}} end % 14.5 local L in L = [[[nil a] nil]] try {Browse 5#{LeftMost L}} catch _ then skip end end % 14.23 fun {LeftMost_1 L} case L of nil then nil [] H|T then if {IsAtomS H} then H else if {IsAtomS {LeftMost_1 H}} then {LeftMost_1 H} else {LeftMost_1 T} end end end end % 14.14 local L in L = [[[nil a] nil]] {Browse 14#{LeftMost_1 L}} end % 14.17 {Browse 17#{LeftMost_1 nil}} % 14.24 local L in L = [[[a] b] [c d]] {Browse 24#{LeftMost_1 L}} end % 14.25 local L in L = [[[a] nil] nil [e]] {Browse 25#{LeftMost_1 L}} end % 14.26 local L in L = [[[nil a] nil]] {Browse 26#{LeftMost_1 L}} end % 14.23 fun {LeftMost_2 L} case L of nil then nil [] H|T then if {IsAtomS H} then H else local A = {LeftMost_2 H} in if {IsAtomS A} then A else {LeftMost_2 T} end end end end end % 14.37 fun {Rember1Star L A} case L of nil then nil [] H|T then if {IsAtomS H} then if H == A then T else H|{Rember1Star T A} end else if {EqList H {Rember1Star H A}} then H|{Rember1Star T A} else {Rember1Star H A}|T end end end end fun {Rember1Star_1 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 % 14.35 local A L in A = salad L = [['Swedish' rye] ['French' [mustard salad turkey]] salad] {Browse 35#{Rember1Star_1 L A}} end % 14.36 local A L in A = meat L = [[pasta meat] pasta [noodle meat sauce] meat tomatoes] {Browse 36#{Rember1Star_1 L A}} end % 14.43 fun {Rember1Star_2 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 local Av = {R H} in if {EqList H Av} then H|{R T} else Av|T end end end end end in {R L} end % 14.49 fun {DepthStar L} case L of nil then 1 [] H|T then if {IsAtomS H} then {DepthStar T} else if {DepthStar T} > {Add1 {DepthStar H}} then {DepthStar T} else {Add1 {DepthStar H}} end end end end % 14.50 local L in L = [[pickled] peppers [peppers pickled]] {Browse 50#{DepthStar L}} end % 14.51 local L in L = [margarine [[bitter butter] [makes] [batter [bitter]]] butter] {Browse 51#{DepthStar L}} end % 14.52 local L in L = [c [b [a b] a] a] {Browse 52#{DepthStar L}} end % 14.56 fun {DepthStar_1 L} local A D in A = {Add1 {DepthStar_1 L.1}} D = {DepthStar_1 L.2} case L of nil then 1 [] H|T then if {IsAtomS H} then D else if D > A then D else A end end end end end % 14.58 local L in L = [nil [[bitter butter] [makes] [batter [bitter]]] butter] try {Browse 58#{DepthStar_1 L}} catch _ then skip end end % 14.66 fun {DepthStar_2 L} case L of nil then 1 [] H|T then if {IsAtomS H} then {DepthStar_2 T} else local A D in A = {Add1 {DepthStar_2 H}} D = {DepthStar_2 T} if D > A then D else A end end end end end % 14.72 fun {DepthStar_3 L} case L of nil then 1 [] H|T then local D in D = {DepthStar_3 T} if {IsAtomS H} then {DepthStar_3 T} else if D > {Add1 {DepthStar_3 H}} then D else {Add1 {DepthStar_3 H}} end end end end end % 14.73 fun {DepthStar_4 L} case L of nil then 1 [] H|T then local D = {DepthStar_4 T} in if {IsAtomS H} then D else local A = {Add1 {DepthStar_4 H}} in if D > A then D else A end end end end end end % 14.82 % Oz doesn't have if shortcut used in this example % 14.86 fun {DepthStar_5 L} case L of nil then 1 [] H|T then if {IsAtomS H} then {DepthStar_5 T} else local A D in A = {Add1 {DepthStar_5 H}} D = {DepthStar_5 T} {Max A D} end end end end fun {DepthStar_6 L} case L of nil then 1 [] H|T then if {IsAtomS H} then {DepthStar_6 T} else {Max {Add1 {DepthStar_6 H}} {DepthStar_6 T}} end end end % 14.87 fun {Scramble Tup} fun {P Tup Rp} case Tup of nil then nil [] H|T then local Rp = H|Rp in {Nth Rp H} | {P T Rp} end end end in {P Tup nil} end % 14.91 fun {LeftMost_3 L} case L of nil then nil [] H|T then if {IsAtomS H} then H else local A = {LeftMost_3 H} in if {IsAtomS A} then A else {LeftMost_3 T} end end end end end % 14.92 local L in L = [[[a]] b [c]] {Browse 92#{LeftMost_3 L}} end % 14.104 fun {LeftMost_4 L} fun {Skip} try {Lm L 'skip'} catch 'skip'(E) then E end end in {Skip} end fun {Lm L Out} case L of nil then nil [] H|T then if {IsAtomS H} then raise Out(H) end else _ = {Lm H Out} {Lm T Out} end end end % 14.108 local L in L = [[[a]] b [c]] {Browse 108#{LeftMost_4 L}} end % 14.120 fun {LeftMost_5 L} fun {Lm L Out} case L of nil then nil [] H|T then if {IsAtomS H} then raise Out(H) end else _ = {Lm H Out} {Lm T Out} end end end fun {Skip} try {Lm L 'skip'} catch 'skip'(E) then E end end in {Skip} end % 14.121 fun {LeftMost_6 L} fun {Skip} fun {Lm L Out} case L of nil then nil [] H|T then if {IsAtomS H} then raise Out(H) end else _ = {Lm H Out} {Lm T Out} end end end in try {Lm L 'skip'} catch 'skip'(E) then E end end in {Skip} end % 14.124 fun {LeftMost_7 L} fun {Skip} fun {Lm L Skip} case L of nil then nil [] H|T then if {IsAtomS H} then raise Skip(H) end else _ = {Lm H Skip} {Lm T Skip} end end end in try {Lm L 'skip'} catch 'skip'(E) then E end end in {Skip} end % 14.125 fun {LeftMost_8 L} fun {Skip} fun {Lm L} case L of nil then nil [] H|T then if {IsAtomS H} then raise 'skip'(H) end else _ = {Lm H} {Lm T} end end end in try {Lm L} catch 'skip'(E) then E end end in {Skip} end % 14#143 fun {Rm L A Oh} case L of nil then raise Oh(no) end [] H|T then if {IsAtomS H} then if H == A then T else H|{Rm T A Oh} end else local X in X = try {Rm H A oh} catch oh(E) then H end if {IsAtomS X} then H|{Rm T A Oh} else {Rm H A Oh}|T end end end end end % 14.146 local A L in A = noodles L = [[food] more [food]] {Browse 146#try {Rm L A say} catch say(E) then E end} end % 14.155 fun {Rember1Star_3 L A} X in X = try {Rm L A say} catch say(E) then E end if {IsAtomS X} then L else {Rm L A nil} end end % 14.157 fun {Rember1Star_4 L A} X in X = try {Rm_1 L A say} catch say(E) then E end if {IsAtomS X} then L else X end end fun {Rm_1 L A Oh} case L of nil then raise Oh(no) end [] H|T then if {IsAtomS H} then if H == A then T else H|{Rm_1 T A Oh} end else local X in X = try {Rm_1 H A oh} catch oh(E) then E end if {IsAtomS X} then H|{Rm_1 T A Oh} else X|T end end end end end % 14.161 fun {Rember1Star_5 L A} try {Rm_2 L A say} catch say(E) then L end end % 14.163 fun {Rm_2 L A Oh} case L of nil then raise Oh(no) end [] H|T then if {IsAtomS H} then if H == A then T else H|{Rm_2 T A Oh} end else try {Rm_2 H A oh}|T catch oh(_) then H|{Rm_2 T A Oh} end end end end |