TLS Chapter #09 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 {First P} P.1 end fun {Second P} P.2.1 end fun {Build S1 S2} S1|S2|nil end fun {IsPair P} case P of _|_|nil then true else false end end fun {RevPair Pair} {Build {Second Pair} {First Pair}} end %%%%%%%%%%%%%%%%%%% The Little Schemer - Chapter - 9 %%%%%%%%%%%%%%%%%%%%%% % 9.11 fun {Pick L N} {Nth L N} end local Lat in Lat = [6 2 4 caviar 5 7 3] {Browse 11#{Nth Lat 6}} end % 9.13 local Lat in Lat = [6 2 4 caviar 5 7 3] {Browse 13#{Nth Lat 7}} end % 9.16 fun {KeepLooking L A Sorn} if {IsNumber Sorn} then {KeepLooking L A {Nth L Sorn}} else Sorn == A end end % 9.14 local A Lat in A = caviar Lat = [6 2 4 caviar 5 7 3] {Browse 14#{KeepLooking Lat A 3}} end % 9.9 fun {Looking L A } {KeepLooking L A {Nth L 1}} end % 9.2 local A Lat in A = caviar Lat = [6 2 4 caviar 5 7 3] {Browse 2#{Looking Lat A}} end % 9.3 local A Lat in A = caviar Lat = [6 2 grits caviar 5 7 3] {Browse 3#{Looking Lat A}} end % 9.10 local A Lat in A = caviar Lat = [6 2 4 caviar 5 7 3] {Browse 10#{Looking Lat A}} end % 9.23 local A Lat in A = caviar Lat = [7 1 2 caviar 5 6 3] %{Browse 23#{Looking Lat A}} % infinite loop end % 9.26 fun {Eternity X} {Eternity X} end % 9.31 fun {Shift Pair} {Build {First {First Pair}} {Build {Second {First Pair}} {Second Pair}}} end % 9.29 local X in X = [[a b] c] {Browse 29#{Shift X}} end % 9.30 local X in X = [[a b] [c d]] {Browse 29#{Shift X}} end % 9.33 fun {Align Pora} if {IsAtomS Pora} then Pora else if {IsPair Pora} then {Align {Shift Pora}} else {Build {First Pora} {Align {Second Pora}}} end end end % 9.39 fun {LengthStar Pora} if {IsAtomS Pora} then 1 else {LengthStar {First Pora}} + {LengthStar {Second Pora}} end end % 9.45 fun {WeightStar Pora} if {IsAtomS Pora} then 1 else {WeightStar {First Pora}}*2 + {WeightStar {Second Pora}} end end % 9.46 local X in X = [[a b] c] {Browse 46#{WeightStar X}} end % 9.47 local X in X = [a [b c]] {Browse 46#{WeightStar X}} end % 9.50 fun {Shuffle Pora} if {IsAtomS Pora} then Pora else if {IsPair {First Pora}} then {Shuffle {RevPair Pora}} else {Build {First Pora} {Shuffle {Second Pora}}} end end end % 9.52 local X in X = [a [b c]] {Browse 52#{Shuffle X}} end % 9.53 local X in X = [a b] {Browse 53#{Shuffle X}} end % 9.54 local X in X = [[a b] [c d]] %{Browse 54#{Shuffle X}} % infinite loop end % 9.58 fun {C N} if N == 1 then 1 else if {IsEven N} then {C N div 2} else {C {Add1 3*N}} end end end % 9.62 fun {A N M} if N == 0 then {Add1 M} elseif M == 0 then {A {Sub1 N} 1} else {A {Sub1 N} {A N {Sub1 M}}} end end % 9.59 {Browse 59#{A 1 0}} % 9.60 {Browse 60#{A 1 1}} % 9.61 {Browse 61#{A 2 2}} % 9.66 % {Browse 66#{A 4 3}} % takes a long, long time % 9.71 % fun {WillStop F} ... end % 9.79 % fun {LastTry X} % {WillStop LastTry} andthen {Eternity X} % end % 9.94 fun {Length_ L} case L of nil then 0 [] _|T then {Add1 {Length_ T}} end end % 9.96 _ = fun {$ L} case L of nil then 0 [] _|T then {Add1 {Eternity T}} end end % 9.100 % _ = fun {$ L} % case L % of nil then 0 % [] _|T then {Add1 {Length0 T}} % end % end % 9.101 _ = fun {$ L} case L of nil then 0 [] _|T then {Add1 {fun {$ L} case L of nil then 0 [] _|T then {Add1 {Eternity T}} end end T}} end end % 9.103 _ = fun {$ L} case L of nil then 0 [] _|T then {Add1 {fun {$ L} case L of nil then 0 [] _|T then {Add1 {fun {$ L} case L of nil then 0 [] _|T then {Add1 {Eternity T}} end end T}} end end T}} end end % 9.111 _ = {fun {$ Length} fun {$ L} case L of nil then 0 [] _|T then {Add1 {Length T}} end end end Eternity} % 9.112 _ = {fun {$ F} fun {$ L} case L of nil then 0 [] _|T then {Add1 {F T}} end end end {fun {$ G} fun {$ L} case L of nil then 0 [] _|T then {Add1 {G T}} end end end Eternity}} % 9.114 _ = {fun {$ Length} fun {$ L} case L of nil then 0 [] _|T then {Add1 {Length T}} end end end {fun {$ Length} fun {$ L} case L of nil then 0 [] _|T then {Add1 {Length T}} end end end {fun {$ Length} fun {$ L} case L of nil then 0 [] _|T then {Add1 {Length T}} end end end Eternity}}} % 9.118 _ = {fun {$ MkLength} {MkLength Eternity} end fun {$ Length} fun {$ L} case L of nil then 0 [] _|T then {Add1 {Length T}} end end end} % 9.119 _ = {fun {$ MkLength} {MkLength {MkLength Eternity}} end fun {$ Length} fun {$ L} case L of nil then 0 [] _|T then {Add1 {Length T}} end end end} _ = {fun {$ MkLength} {MkLength {MkLength {MkLength Eternity}}} end fun {$ Length} fun {$ L} case L of nil then 0 [] _|T then {Add1 {Length T}} end end end} % 9.120 _ = {fun {$ MkLength} {MkLength {MkLength {MkLength {MkLength Eternity}}}} end fun {$ Length} fun {$ L} case L of nil then 0 [] _|T then {Add1 {Length T}} end end end} % 9.127 % Note: these do not work in Oz for non-empty list _ = {fun {$ MkLength} {MkLength MkLength} end fun {$ Length} fun {$ L} case L of nil then 0 [] _|T then {Add1 {Length T}} end end end} _ = {fun {$ MkLength} {MkLength MkLength} end fun {$ MkLength} fun {$ L} case L of nil then 0 [] _|T then {Add1 {MkLength T}} end end end} % 9.130 % Note: this does not work in Oz for list with length greater than one _ = {fun {$ MkLength} {MkLength MkLength} end fun {$ MkLength} fun {$ L} case L of nil then 0 [] _|T then {Add1 {{MkLength Eternity} T}} end end end} % 9.131 local L in L = [apples] {Browse 131#{{fun {$ MkLength} {MkLength MkLength} end fun {$ MkLength} fun {$ L} case L of nil then 0 [] _|T then {Add1 {{MkLength Eternity} T}} end end end} L}} end % 9.133 _ = {fun {$ MkLength} {MkLength MkLength} end fun {$ MkLength} fun {$ L} case L of nil then 0 [] _|T then {Add1 {{MkLength MkLength} T}} end end end} % 9.137 % Note: this is an infinite loop % _ = {fun {$ MkLength} % {MkLength MkLength} % end % fun {$ MkLength} % {fun {$ Length} % fun {$ L} % case L % of nil then 0 % [] _|T then {Add1 {Length T}} % end % end % end {MkLength MkLength}} % end} % 9.139 local L in L = [apples] % {Browse 139#{{fun {$ MkLength} % {MkLength MkLength} % end % fun {$ MkLength} % {fun {$ Length} % fun {$ L} % case L % of nil then 0 % [] _|T then {Add1 {Length T}} % end % end % end {MkLength MkLength}} % end} L}} end % 9.149 _ = {fun {$ MkLength} {MkLength MkLength} end fun {$ MkLength} fun {$ L} case L of nil then 0 [] _|T then {Add1 {fun {$ X} {{MkLength MkLength} X} end T}} end end end} % 9.150 _ = {fun {$ MkLength} {MkLength MkLength} end fun {$ MkLength} {fun {$ Length} fun {$ L} case L of nil then 0 [] _|T then {Add1 {Length T}} end end end fun {$ X} {{MkLength MkLength} X} end} end} % 9.153 _ = {fun {$ Le} {fun {$ MkLength} {MkLength MkLength} end fun {$ MkLength} {Le fun {$ X} {{MkLength MkLength} X} end} end} end fun {$ Length} fun {$ L} case L of nil then 0 [] _|T then {Add1 {Length T}} end end end} % 9.155 _ = fun {$ Le} {fun {$ MkLength} {MkLength MkLength} end fun {$ MkLength} {Le fun {$ X} {{MkLength MkLength} X} end} end} end % 9.156 Y = fun {$ Le} {fun {$ F} {F F} end fun {$ F} {Le fun {$ X} {{F F} X} end} end} end % 9.159 % {Browse 159#{Y Y}} % infinite loop |