00001
00002
00003
00004
00005 import OpenListException;
00006
00007
00008
00009
00010
00011
00012
00013 class OpenList
00014 {
00015
00016
00017
00018
00019
00020
00021
00022
00023 public static String defaultLeftParen = "[";
00024
00025
00026
00027
00028 public static String defaultRightParen = "]";
00029
00030
00031
00032
00033 public static String defaultSpacer = ", ";
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 final public static OpenList nil = cons(null, null);
00044
00045
00046
00047
00048 public static String firstOfOpenListException = "first() of empty list";
00049
00050
00051
00052
00053 public static String restOfOpenListException = "rest() of empty list";
00054
00055
00056
00057
00058 private Object First;
00059
00060
00061
00062
00063 private OpenList Rest;
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 private OpenList(Object _First, OpenList _Rest)
00075 {
00076 First = _First;
00077 Rest = _Rest;
00078 }
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 public static OpenList cons(Object _First, OpenList _Rest)
00090 {
00091 return new OpenList(_First, _Rest);
00092 }
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 public Object first() throws OpenListException
00103 {
00104 if( isEmpty() )
00105 {
00106 throw new OpenListException(firstOfOpenListException);
00107 }
00108 return First;
00109 }
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 public static Object first(OpenList List) throws OpenListException
00121 {
00122 return List.first();
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 public OpenList rest() throws OpenListException
00136 {
00137 if( isEmpty() )
00138 {
00139 throw new OpenListException(restOfOpenListException);
00140 }
00141 return Rest;
00142 }
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 public static OpenList rest(OpenList List) throws OpenListException
00155 {
00156 return List.rest();
00157 }
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167 public boolean isEmpty()
00168 {
00169 return this == nil;
00170 }
00171
00172
00173
00174
00175
00176
00177
00178 public boolean nonEmpty()
00179 {
00180 return this != nil;
00181 }
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 public static boolean isEmpty(OpenList List)
00194 {
00195 return List.isEmpty();
00196 }
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 public static boolean nonEmpty(OpenList List)
00207 {
00208 return List.nonEmpty();
00209 }
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 public static OpenList append(OpenList L1, OpenList L2)
00223 {
00224 if( L1.isEmpty() )
00225 {
00226 return L2;
00227 }
00228 else
00229 {
00230 return cons(L1.first(), append(L1.rest(), L2));
00231 }
00232 }
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243 public static OpenList reverse(OpenList L1)
00244 {
00245 OpenList result = nil;
00246
00247 for( ; L1.nonEmpty(); L1 = L1.rest() )
00248 {
00249 result = cons(L1.first(), result);
00250 }
00251
00252 return result;
00253 }
00254
00255
00256
00257
00258
00259
00260
00261
00262 public int length()
00263 {
00264 OpenList L = this;
00265 int result = 0;
00266 for( ; L.nonEmpty(); L = L.rest() )
00267 {
00268 result++;
00269 }
00270
00271 return result;
00272 }
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285 public Object nth(int n) throws OpenListException
00286 {
00287 int remaining = n;
00288 OpenList L = this;
00289
00290 try
00291 {
00292 while( remaining > 0 )
00293 {
00294 L = L.rest();
00295 remaining--;
00296 }
00297 return L.first();
00298 }
00299 catch( OpenListException e )
00300 {
00301 throw new OpenListException("nth of an OpenList of length " + length()
00302 + ", where n == " + n);
00303 }
00304 }
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318 public OpenList prefix(int n)
00319 {
00320 if( isEmpty() || n <= 0 )
00321 {
00322 return nil;
00323 }
00324
00325 return cons(first(), rest().prefix(n-1));
00326 }
00327
00328
00329
00330
00331
00332
00333
00334
00335 public String toString()
00336 {
00337 return toString(defaultLeftParen, defaultSpacer, defaultRightParen);
00338 }
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352 public String toString(String leftParen,
00353 String spacer,
00354 String rightParen)
00355 {
00356 StringBuffer buffer = new StringBuffer();
00357
00358 buffer.append(leftParen);
00359
00360 if( nonEmpty() )
00361 {
00362 buffer.append(first().toString());
00363 OpenList remainder = rest();
00364
00365 while( remainder.nonEmpty() )
00366 {
00367 buffer.append(spacer);
00368 buffer.append(remainder.first());
00369 remainder = remainder.rest();
00370 }
00371 }
00372
00373 buffer.append(rightParen);
00374 return buffer.toString();
00375 }
00376
00377
00378
00379
00380
00381
00382
00383 public static void main(String arg[])
00384 {
00385 try
00386 {
00387 OpenList L0 = nil;
00388
00389 System.out.println("L0 = " + L0);
00390
00391 OpenList L1 = cons("a", cons("b", cons("c", nil)));
00392
00393 System.out.println("L1 = " + L1);
00394
00395 OpenList L2 = cons(cons("b", cons("c", nil)), cons("d", nil));
00396
00397 System.out.println("L2 = " + L2);
00398
00399 OpenList L3 = cons(L1, cons(L2, nil));
00400
00401 System.out.println("L3 = " + L3);
00402
00403 OpenList L4 = cons(L1, L2);
00404
00405 System.out.println("L4 = " + L4);
00406
00407 OpenList L5 = cons(L2, L1);
00408
00409 System.out.println("L5 = " + L5);
00410
00411 OpenList L6 = cons(new Integer(1), nil);
00412
00413 System.out.println("L6 = " + L6);
00414
00415 OpenList L7 = cons(new Integer(1),
00416 cons(new Integer(2),
00417 cons(new Integer(3),
00418 cons(new Integer(4),
00419 nil))));
00420
00421 System.out.println("L7 = " + L7);
00422
00423 System.out.println("append(L1, L2) = " + append(L1, L2));
00424
00425 System.out.println("reverse(L5) = " + reverse(L5));
00426
00427 System.out.println("append(reverse(L7), reverse(L1)) = "
00428 + append(reverse(L7), reverse(L1)));
00429
00430 System.out.println("reverse(append(L1, L7)) = "
00431 + reverse(append(L1, L7)));
00432
00433 System.out.println("L5.length() = " + L5.length());
00434
00435 System.out.println("L5.nth(2) = " + L5.nth(2));
00436
00437 System.out.println("L5.prefix(3) = " + L5.prefix(3));
00438
00439 System.out.println("L5.nth(4) = " + L5.nth(4));
00440 }
00441 catch( Exception e )
00442 {
00443 System.err.println("*** caught: " + e);
00444 }
00445 }
00446
00447 }