00001
00002
00003
00004
00005 import java.io.*;
00006 import java.util.*;
00007
00008
00009
00010
00011
00012
00013
00014 class OpenList implements Cloneable
00015 {
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 final public static OpenList nil = cons(null, null);
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 public static String defaultLeftParen = "[";
00037
00038
00039
00040
00041 public static String defaultRightParen = "]";
00042
00043
00044
00045
00046 public static String defaultSpacer = ", ";
00047
00048
00049
00050
00051 public static String firstException = "first() of empty list";
00052
00053
00054
00055
00056 public static String restException = "rest() of empty list";
00057
00058
00059
00060
00061 public static String nthException = "nth of an OpenList of length ";
00062
00063
00064
00065
00066 public static String wherePart = ", where n == ";
00067
00068
00069
00070
00071 public static String secondException =
00072 "second of an OpenList, where length() = ";
00073
00074
00075
00076
00077 public static String thirdException = "third of an OpenList, where length() = ";
00078
00079
00080
00081
00082 private Object First;
00083
00084
00085
00086
00087 private OpenList Rest;
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 private OpenList(Object _First, OpenList _Rest)
00099 {
00100 First = _First;
00101 Rest = _Rest;
00102 }
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 public static OpenList cons(Object _First, OpenList _Rest)
00114 {
00115 return new OpenList(_First, _Rest);
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 public OpenList cons(Object _First)
00127 {
00128 return new OpenList(_First, this);
00129 }
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 public Object first() throws OpenListException
00140 {
00141 if( isEmpty() )
00142 {
00143 throw new OpenListException(firstException);
00144 }
00145 return First;
00146 }
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 public static Object first(OpenList List) throws OpenListException
00158 {
00159 return List.first();
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 public OpenList rest() throws OpenListException
00172 {
00173 if( isEmpty() )
00174 {
00175 throw new OpenListException(restException);
00176 }
00177 return Rest;
00178 }
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 public static OpenList rest(OpenList List) throws OpenListException
00191 {
00192 return List.rest();
00193 }
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203 public boolean isEmpty()
00204 {
00205 return this == nil;
00206 }
00207
00208
00209
00210
00211
00212
00213
00214 public boolean nonEmpty()
00215 {
00216 return this != nil;
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 public static boolean isEmpty(OpenList List)
00230 {
00231 return List.isEmpty();
00232 }
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242 public static boolean nonEmpty(OpenList List)
00243 {
00244 return List.nonEmpty();
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258 public static OpenList append(OpenList L1, OpenList L2)
00259 {
00260 if( L1.isEmpty() )
00261 {
00262 return L2;
00263 }
00264 else
00265 {
00266 return cons(L1.first(), append(L1.rest(), L2));
00267 }
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 public OpenList append(OpenList L2)
00281 {
00282 return append(this, L2);
00283 }
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 public static boolean member(Object Ob, OpenList L)
00297 {
00298 for( ; L.nonEmpty(); L = L.rest() )
00299 {
00300 if( Ob.equals(L.first()) ) return true;
00301 }
00302
00303 return false;
00304 }
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316 public boolean member(Object Ob)
00317 {
00318 return member(Ob, this);
00319 }
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330 public static OpenList reverse(OpenList L1)
00331 {
00332 OpenList result = nil;
00333
00334 for( ; L1.nonEmpty(); L1 = L1.rest() )
00335 {
00336 result = cons(L1.first(), result);
00337 }
00338
00339 return result;
00340 }
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 public OpenList reverse()
00351 {
00352 return reverse(this);
00353 }
00354
00355
00356
00357
00358
00359
00360
00361
00362 public static int length(OpenList L)
00363 {
00364 int result = 0;
00365
00366 for( ; L.nonEmpty(); L = L.rest() )
00367 {
00368 result++;
00369 }
00370
00371 return result;
00372 }
00373
00374
00375
00376
00377
00378
00379
00380
00381 public int length()
00382 {
00383 return length(this);
00384 }
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399 public static Object nth(int n, OpenList L) throws OpenListException
00400 {
00401 int remaining = n;
00402 OpenList original = L;
00403
00404 try
00405 {
00406 while( remaining > 0 )
00407 {
00408 L = L.rest();
00409 remaining--;
00410 }
00411 return L.first();
00412 }
00413 catch( OpenListException e )
00414 {
00415 throw new OpenListException(nthException + original.length()
00416 + wherePart + n);
00417 }
00418 }
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430 public Object nth(int n) throws OpenListException
00431 {
00432 return nth(n, this);
00433 }
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445 public OpenList prefix(int n)
00446 {
00447 if( isEmpty() || n <= 0 )
00448 {
00449 return nil;
00450 }
00451
00452 return cons(first(), rest().prefix(n-1));
00453 }
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466 static public OpenList prefix(int n, OpenList L)
00467 {
00468 return L.prefix(n);
00469 }
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479 public Object second() throws OpenListException
00480 {
00481 try
00482 {
00483 return nth(1);
00484 }
00485 catch( OpenListException e )
00486 {
00487 throw new OpenListException(thirdException + length());
00488 }
00489 }
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499 public Object third() throws OpenListException
00500 {
00501 try
00502 {
00503 return nth(2);
00504 }
00505 catch( OpenListException e )
00506 {
00507 throw new OpenListException(thirdException + length());
00508 }
00509 }
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520 public static Object second(OpenList L) throws OpenListException
00521 {
00522 return L.second();
00523 }
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534 public static Object third(OpenList L) throws OpenListException
00535 {
00536 return L.third();
00537 }
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547 public static OpenList list()
00548 {
00549 return nil;
00550 }
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560 public static OpenList list(Object First)
00561 {
00562 return cons(First, nil);
00563 }
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574 public static OpenList list(Object First, Object Second)
00575 {
00576 return cons(First, list(Second));
00577 }
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590 public static OpenList list(Object First, Object Second, Object Third)
00591 {
00592 return cons(First, list(Second, Third));
00593 }
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606 public static OpenList list(Object First,
00607 Object Second,
00608 Object Third,
00609 Object Fourth)
00610 {
00611 return cons(First, list(Second, Third, Fourth));
00612 }
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626 public static OpenList list(Object First,
00627 Object Second,
00628 Object Third,
00629 Object Fourth,
00630 Object Fifth)
00631 {
00632 return cons(First, list(Second, Third, Fourth, Fifth));
00633 }
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648 public static OpenList list(Object First,
00649 Object Second,
00650 Object Third,
00651 Object Fourth,
00652 Object Fifth,
00653 Object Sixth)
00654 {
00655 return cons(First, list(Second, Third, Fourth, Fifth, Sixth));
00656 }
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672 public static OpenList list(Object First,
00673 Object Second,
00674 Object Third,
00675 Object Fourth,
00676 Object Fifth,
00677 Object Sixth,
00678 Object Seventh)
00679 {
00680 return cons(First, list(Second, Third, Fourth, Fifth, Sixth, Seventh));
00681 }
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693 public static boolean equals(OpenList L1, OpenList L2)
00694 {
00695
00696
00697
00698 while( L1.nonEmpty() && L2.nonEmpty() )
00699 {
00700 if( !(L1.first().equals(L2.first())) )
00701 {
00702 return false;
00703 }
00704
00705 L1 = L1.rest();
00706 L2 = L2.rest();
00707 }
00708
00709
00710
00711
00712 return L1.isEmpty() && L2.isEmpty();
00713 }
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723 public boolean equals(Object Ob)
00724 {
00725 if( Ob instanceof OpenList )
00726 {
00727 return equals(this, (OpenList)Ob);
00728 }
00729 return false;
00730 }
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741 public Object clone()
00742 {
00743 if( isEmpty() )
00744 {
00745 return nil;
00746 }
00747 return cons(Copier.maybeClone(first()), (OpenList)rest().clone());
00748 }
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759 public static OpenList map(Function1 f, OpenList L)
00760 {
00761 if( L.isEmpty() )
00762 {
00763 return nil;
00764 }
00765 else
00766 {
00767 return cons(f.apply(L.first()), map(f, L.rest()));
00768 }
00769 }
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779 public OpenList map(Function1 f)
00780 {
00781 return map(f, this);
00782 }
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796 public static OpenList mappend(Function1 f, OpenList L)
00797 {
00798 if( L.isEmpty() )
00799 {
00800 return nil;
00801 }
00802 else
00803 {
00804 return append((OpenList)f.apply(L.first()), mappend(f, L.rest()));
00805 }
00806 }
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819 public static Object reduce(Function2 b, Object u, OpenList L)
00820 {
00821 Object result = u;
00822 for( ; L.nonEmpty(); L = L.rest() )
00823 {
00824 result = b.apply(result, L.first());
00825 }
00826 return result;
00827 }
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839 public Object reduce(Function2 b, Object u)
00840 {
00841 return reduce(b, u, this);
00842 }
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859 public static OpenList assoc(Object X, OpenList L)
00860 {
00861 for( ; L.nonEmpty(); L = L.rest() )
00862 {
00863 OpenList First = (OpenList)L.first();
00864
00865 if( X.equals(First.first()) )
00866 {
00867 return First;
00868 }
00869 }
00870
00871 return nil;
00872 }
00873
00874
00875
00876
00877
00878
00879
00880
00881 public String toString()
00882 {
00883 return toString(defaultLeftParen, defaultSpacer, defaultRightParen);
00884 }
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896 public String toString(String leftParen,
00897 String spacer,
00898 String rightParen)
00899 {
00900 StringBuffer buffer = new StringBuffer();
00901
00902 buffer.append(leftParen);
00903
00904 if( nonEmpty() )
00905 {
00906 buffer.append(first().toString());
00907 OpenList remainder = rest();
00908
00909 while( remainder.nonEmpty() )
00910 {
00911 buffer.append(spacer);
00912 buffer.append(remainder.first());
00913 remainder = remainder.rest();
00914 }
00915 }
00916
00917 buffer.append(rightParen);
00918 return buffer.toString();
00919 }
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930 public static OpenList explode(String s)
00931 {
00932 OpenList result = nil;
00933
00934 for( int i = s.length()-1; i >= 0; i-- )
00935 {
00936 result = cons(new Character(s.charAt(i)), result);
00937 }
00938
00939 return result;
00940 }
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955 public static String implode(OpenList L)
00956 {
00957 StringBuffer buffer = new StringBuffer();
00958
00959 for( ; L.nonEmpty(); L = L.rest() )
00960 {
00961 buffer.append(L.first().toString());
00962 }
00963
00964 return buffer.toString();
00965 }
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978 public String implode()
00979 {
00980 return implode(this);
00981 }
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991 public static Object[] toArray(OpenList L)
00992 {
00993 Object a[] = new Object[L.length()];
00994
00995 int i = 0;
00996
00997 for( ; L.nonEmpty(); L = L.rest() )
00998 {
00999 a[i] = L.first();
01000 i++;
01001 }
01002 return a;
01003 }
01004
01005
01006
01007
01008
01009
01010
01011
01012 public Object[] toArray()
01013 {
01014 return toArray(this);
01015 }
01016
01017
01018
01019
01020
01021
01022
01023
01024 public static OpenList fromArray(Object a[])
01025 {
01026 OpenList result = nil;
01027
01028 for( int i = a.length-1; i >= 0; i-- )
01029 {
01030 result = cons(a[i], result);
01031 }
01032
01033 return result;
01034 }
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047 public OpenList sort(java.util.Comparator comparator)
01048 {
01049 Object a[] = this.toArray();
01050
01051 Quicksort q = new Quicksort(a);
01052
01053 q.sort(comparator);
01054
01055 return fromArray(a);
01056 }
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067 public static OpenList readLines(BufferedReader reader) throws IOException
01068 {
01069 String stringRead = reader.readLine();
01070
01071 if( stringRead == null )
01072 return nil;
01073
01074 return cons(stringRead, readLines(reader));
01075 }
01076
01077
01078
01079
01080
01081
01082
01083 public Enumeration elements()
01084 {
01085 return new OpenListEnumeration(this);
01086 }
01087
01088
01089
01090
01091
01092
01093
01094
01095 public static OpenList solidify(Enumeration E)
01096 {
01097 if( E.hasMoreElements() )
01098 {
01099 return cons(E.nextElement(), solidify(E));
01100 }
01101 else
01102 {
01103 return nil;
01104 }
01105 }
01106
01107
01108
01109
01110
01111
01112
01113 public static void main(String arg[])
01114 {
01115 Integer zero = new Integer(0);
01116 Integer one = new Integer(1);
01117 Integer two = new Integer(2);
01118 Integer three = new Integer(3);
01119 Integer four = new Integer(4);
01120
01121 try
01122 {
01123 System.out.println("zero = " + zero );
01124 System.out.println("one = " + one );
01125 System.out.println("two = " + two );
01126 System.out.println("three = " + three);
01127 System.out.println("four = " + four );
01128
01129 OpenList L0 = nil;
01130
01131 System.out.println("L0 = " + L0);
01132
01133 OpenList L1 = cons("a", cons("b", cons("c", nil)));
01134
01135 System.out.println("L1 = " + L1);
01136
01137 OpenList L2 = cons(cons("b", cons("c", nil)), cons("d", nil));
01138
01139 System.out.println("L2 = " + L2);
01140
01141 OpenList L3 = cons(L1, cons(L2, nil));
01142
01143 System.out.println("L3 = " + L3);
01144
01145 OpenList L4 = cons(L1, L2);
01146
01147 System.out.println("L4 = " + L4);
01148
01149 OpenList L5 = cons(L2, L1);
01150
01151 System.out.println("L5 = " + L5);
01152
01153 OpenList L6 = cons(one, nil);
01154
01155 System.out.println("L6 = " + L6);
01156
01157 OpenList L7 = cons(one, cons(two, cons(three, cons(four, nil))));
01158
01159 System.out.println("L7 = " + L7);
01160
01161 System.out.println("append(L1, L2) = " + append(L1, L2));
01162
01163 System.out.println("reverse(L5) = " + reverse(L5));
01164
01165 System.out.println("append(reverse(L7), reverse(L1)) = "
01166 + append(reverse(L7), reverse(L1)));
01167
01168 System.out.println("reverse(append(L1, L7)) = "
01169 + reverse(append(L1, L7)));
01170
01171 System.out.println("L5.length() = " + L5.length());
01172
01173 System.out.println("L5.nth(2) = " + L5.nth(2));
01174
01175 System.out.println("L5.prefix(3) = " + L5.prefix(3));
01176
01177 System.out.println("explode(\"Hello, World\") = "
01178 + explode("Hello, World"));
01179
01180 System.out.println("implode(explode(\"Hello, World\")) = "
01181 + implode(explode("Hello, World")));
01182
01183 System.out.println("equals(append(reverse(L1), reverse(L2)), "
01184 + " reverse(append(L2, L1))) = "
01185 + equals(append(reverse(L1), reverse(L2)),
01186 reverse(append(L2, L1))));
01187
01188 System.out.println("L7.equals(reverse(reverse(L7))) = "
01189 + L7.equals(reverse(reverse(L7))));
01190
01191 System.out.println("map(new Scaler(three), L7) = "
01192 + map(new Scaler(three), L7));
01193
01194 System.out.println("map(new Scaler(new Float(3.0)), L7) = "
01195 + map(new Scaler(new Float(3.0)), L7));
01196
01197 System.out.println("reduce(new Multiplier(), one, L7) = "
01198 + reduce(new Multiplier(), one, L7));
01199
01200 System.out.println("reduce(new Multiplier(), new Float(1.0), L7) = "
01201 + reduce(new Multiplier(), new Float(1.0), L7));
01202
01203 System.out.println("map(new Ranger(zero, one), L7) = "
01204 + map(new Ranger(zero, one), L7));
01205
01206 System.out.println("mappend(new Ranger(zero, one), L7) = "
01207 + mappend(new Ranger(zero, one), L7));
01208
01209
01210
01211 System.out.println("L5.nth(4) = " + L5.nth(4));
01212
01213 }
01214 catch( Exception e )
01215 {
01216 System.err.println("*** caught: " + e);
01217 }
01218 }
01219 }
01220
01221
01222
01223
01224
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255