Chapter #9 Examples in Oz % Defined in previous chapters fun {EqInt X Y} X == Y end %%%%%%%%%%%%%%%%%%% Chapter - 9 %%%%%%%%%%%%%%%%%%%%%% % 9.9 fun {IsBacon X} case X of bacon then true [] lx(N) then false end end fun {WhereIs1 X} case X of empty then 0 [] cons(ABox Rest) then if {IsBacon ABox} then 1 else 1 + {WhereIs1 Rest} end end end % 9.6 {Browse 6#{WhereIs1 cons(lx(5) cons(lx(13) cons(bacon cons(lx(8) empty))))}} % 9.7 {Browse 7#{WhereIs1 cons(bacon cons(lx(8) empty))}} % 9.10 {Browse 10#{WhereIs1 cons(lx(5) cons(lx(13) cons(lx(8) empty)))}} % 9.14 fun {WhereIs2 X} case X of empty then raise no_bacon(0) end [] cons(ABox Rest) then if {IsBacon ABox} then 1 else 1 + {WhereIs2 Rest} end end end % 9.17 % {Browse 17#{WhereIs2 cons(lx(5) cons(lx(13) cons(lx(8) empty)))}} % 9.31 {Browse 31# try {WhereIs2 cons(lx(5) cons(lx(13) cons(lx(8) empty)))} catch no_bacon(N) then N end} % 9.35 {Browse 35# try {WhereIs2 cons(lx(5) cons(bacon cons(lx(8) empty)))} catch no_bacon(N) then N end} % 9.64 fun {ListItem N L} case L of empty then raise out_of_range end [] cons(ABox Rest) then if {EqInt N 1} then ABox else {ListItem N-1 Rest} end end end % 9.67 fun {Find1 N Boxes} {Check1 N Boxes {ListItem N Boxes}} end fun {Check1 N Boxes ABox} case ABox of bacon then N [] lx(I) then {Find1 I Boxes} end end % 9.75 local T in T = cons(lx(5) cons(lx(4) cons(bacon cons(lx(2) cons(lx(7) empty))))) try {Browse 75#{Find1 1 T}} catch out_of_range then {Browse 75#out_of_range} end end % 9.84 fun {Find2 N Boxes} try {Check2 N Boxes {ListItem N Boxes}} catch out_of_range then {Find2 (N div 2) Boxes} end end fun {Check2 N Boxes ABox} case ABox of bacon then N [] lx(I) then {Find2 I Boxes} end end % 9.86 local T in T = cons(lx(5) cons(lx(4) cons(bacon cons(lx(2) cons(lx(7) empty))))) {Browse 86#{Find2 1 T}} {Browse 86#{Find2 5 T}} end % 9.98 fun {Path N Boxes} try cons(N {Check3 N Boxes {ListItem N Boxes}}) catch out_of_range then {Path (N div 2) Boxes} end end fun {Check3 N Boxes ABox} case ABox of bacon then N [] lx(I) then {Path I Boxes} end end % 9.100 local T in T = cons(lx(5) cons(lx(4) cons(bacon cons(lx(2) cons(lx(7) empty))))) {Browse 100#{Path 1 T}} end % 9.41 local L in L = cons(lx(5) cons(lx(4) cons(bacon cons(lx(2) cons(lx(3) empty))))) {Browse 41#L} end % 9.45 local L in L = cons(lx(5) cons(lx(4) cons(bacon cons(lx(2) cons(lx(3) empty))))) {Browse 45#{Find2 1 L}} end % %% 9.49 % local L in % L = cons(lx(5) cons(lx(4) cons(bacon cons(lx(2) cons(lx(3) empty))))) % {Browse 49#{Find2 2 L}} % end |