TSS Chapter #18 Examples in Oz % Defined in previous chapters fun {IsZero X} X == 0 end fun {Add1 N} N + 1 end fun {Sub1 N} N - 1 end %%%%%%%%%%%%%%%%%%% The Little Schemer - Chapter - 18 %%%%%%%%%%%%%%%%%%%%%% % 18.24 fun {Kons Kar Kdr} X = {NewCell Kar} Y = {NewCell Kdr} in fun {$ Selector} {Selector X Y} end end fun {Kar C} {C fun {$ A D} @A end} end fun {Kdr C} {C fun {$ A D} @D end} end % 18.12 Kounter SetKounter local N = {NewCell 0} in Kounter = fun {$} @N end SetKounter = fun {$ X} N := X @N end fun {KonsC X Y} N := {Add1 @N} {Kons X Y} end end % 18.7 fun {Lots M} if {IsZero M} then nil else {KonsC egg {Lots {Sub1 M}}} end end fun {Lenkth L} if L == nil then 0 else {Add1 {Lenkth {Kdr L}}} end end % 18.8 {Browse 8#{KonsC egg {Lots 3}}} % 18.1 {Browse 1#{Lots 3}} % 18.2 {Browse 2#{Lots 5}} % 18.3 {Browse 3#{Lots 12}} % 18.4 {Browse 4#{Lenkth {Lots 3}}} % 18.5 {Browse 5#{Lenkth {Lots 5}}} % 18.6 {Browse 6#{Lenkth {Lots 15}}} % 18.9 fun {AddAtEnd L} if {Kdr L} == nil then {KonsC {Kar L} {KonsC egg nil}} else {KonsC {Kar L} {AddAtEnd {Kdr L}}} end end % 18.13 _ = {SetKounter 0} {Browse 13#{AddAtEnd {Lots 3}}} % 18.14 {Browse 14#{Kounter}} % 18.16 fun {AddAtEndToo L} proc {A Ls} if {Kdr Ls} == nil then {SetKdr Ls {KonsC egg nil}} else {A {Kdr Ls}} end end in {A L} L end proc {SetKdr C N} {C fun {$ A D} D end} := N end % 18.18 {Browse 18#{SetKounter 0}} % 18.19 {Browse 19#{Kounter}} % 19.20 {Browse 20#{AddAtEndToo {Lots 3}}} % 19.22 {Browse 22#{Kounter}} % 18.25 fun {Bons BKar} A = {NewCell BKar} D = {NewCell nil} in fun {$ Selector} {Selector proc {$ X} D := X end A D} end end fun {BKar C} {C fun {$ S A D} @A end} end fun {BKdr C} {C fun {$ S A D} @D end} end % 19.27 local E in E = egg {Browse 27#{Bons E}} end % 19.30 proc {SetBKdr C X} {{C fun {$ S A D} S end} X} end % 19.32 fun {BKons A D} C = {Bons A} in {SetBKdr C D} C end _ = {SetKounter 0} % 18.36 Dozen = {Lots 12} % 18.37 {Browse 37#{Kounter}} % 18.38 BakersDozen = {AddAtEnd Dozen} % 18.40 {Browse 40#{Kounter}} % 18.41 BakersDozenToo = {AddAtEndToo Dozen} % 18.43 {Browse 43#{Kounter}} % 18.46 BakersDozenAgain = {AddAtEnd Dozen} % 18.49 {Browse 49#{Kounter}} % 18.53 fun {EkList Ls1 Ls2} if Ls1 == nil then Ls2 == nil else if Ls2 == nil then false else {Kar Ls1} == {Kar Ls2} andthen {EkList {Kdr Ls1} {Kdr Ls2}} end end end {Browse 53#{EkList BakersDozen BakersDozenToo}} % 18.60 fun {IsSame C1 C2} T1 = {Kdr C1} T2 = {Kdr C2} V in {SetKdr C1 1} {SetKdr C2 2} V = {Kdr C1} == {Kdr C2} {SetKdr C1 T1} {SetKdr C2 T2} V end % Note: I think the books answer is wrong here {Browse 60#{IsSame BakersDozen BakersDozenToo}} % 18.64 {Browse 64#{IsSame {Kons egg nil} {Kons egg nil}}} % 18.67 fun {LastKons Ls} if {Kdr Ls} == nil then Ls else {LastKons {Kdr Ls}} end end % 18.68 Long = {Lots 12} % 18.70 {SetKdr {LastKons Long} Long} {Browse 70#Long} % 18.71 % {Browse 71#{Lenkth Long}} % 18.72 % {SetKdr {LastKons Long} {Kdr {Kdr Long}}} % 18.81 fun {FiniteLenkth P} fun {C P Q} if {IsSame P Q} then raise infinite end elseif Q == nil then 0 elseif {Kdr Q} == nil then 1 else {C {SL P} {QK Q}} + 2 end end fun {QK X} {Kdr {Kdr X}} end fun {SL X} {Kdr X} end in if P == nil then 0 else {Add1 {C P {Kdr P}}} end end % 18.82 Mongo = {Kons pie {Kons à {Kons la {Kons mode nil}}}} |