00001 // file: OpenList.java 00002 // author: Robert M. Keller 00003 // purpose: Implement Open Lists is java 00004 00005 // note: The comments in this file are javadoc style, used to generate 00006 // API documentation automatically. 00007 00008 import java.io.*; 00009 00010 /** 00011 * OpenList is a type of uni-directional linked list that permits sharing 00012 * of list tails. It implements the list abstraction used in many 00013 * languages, such as rex, Lisp, etc. 00014 */ 00015 00016 class OpenList 00017 { 00018 /** 00019 * the one and only empty OpenList. Do not create any others. 00020 * This list is represented by a unique cell allocated for the purpose. 00021 * The first and rest are not intended to be used. We do not use null 00022 * for this list, so that we can call methods on it. We do not create other 00023 * empty lists so that our implementation of isEmpty() works by comparing 00024 * references. 00025 */ 00026 00027 final public static OpenList nil = cons(null, null); 00028 00029 00030 /** 00031 the default left paren used when a list is printed. 00032 Defaults are used when the toString() method is called, i.e. whenever 00033 an OpenList is automatically cast to a String, as in printing. 00034 To use different punctuation, or no punctuation, use the three-argument 00035 toString, and supply the punctuation you want. 00036 */ 00037 00038 public static String defaultLeftParen = "["; 00039 00040 00041 /** the default right paren used when a list is printed */ 00042 00043 public static String defaultRightParen = "]"; 00044 00045 00046 /** the default space used when a list is printed */ 00047 00048 public static String defaultSpacer = ", "; 00049 00050 00051 /** exception message for first of empty list */ 00052 00053 public static String firstException = "first() of empty list"; 00054 00055 00056 /** exception message for rest of empty list */ 00057 00058 public static String restException = "rest() of empty list"; 00059 00060 00061 /** exception message for nth of a list */ 00062 00063 public static String nthException = "nth of an OpenList of length "; 00064 00065 00066 /** part of exception message for nth of a list */ 00067 00068 public static String wherePart = ", where n == "; 00069 00070 00071 /** exception message for second of a list */ 00072 00073 public static String secondException = "second of an OpenList, where length() = "; 00074 00075 00076 /** exception message for third of a list */ 00077 00078 public static String thirdException = "third of an OpenList, where length() = "; 00079 00080 00081 /** the Object in the first cell of a non-empty list */ 00082 00083 private Object First; 00084 00085 00086 /** the rest of a list, defined to be an OpenList of all but the first cell */ 00087 00088 private OpenList Rest; 00089 00090 /** 00091 * Construct an OpenList from its first and rest. 00092 * This constructor is private because it is intended that the 00093 * pseudo-constructor (or "factory") cons be used outside. 00094 * 00095 * @param _First the first Object in the list 00096 * @param _Rest list of the rest of the objects in the list 00097 */ 00098 00099 private OpenList(Object _First, OpenList _Rest) 00100 { 00101 First = _First; 00102 Rest = _Rest; 00103 } 00104 00105 00106 /** 00107 * Return a new OpenList constructed from its first and rest. 00108 * 00109 * @param _First the first Object in the list 00110 * @param _Rest list of the rest of the objects in the list 00111 * @return new OpenList from first and rest 00112 */ 00113 00114 public static OpenList cons(Object _First, OpenList _Rest) 00115 { 00116 return new OpenList(_First, _Rest); 00117 } 00118 00119 00120 /** 00121 * Return a new OpenList constructed from this list and a first. 00122 * 00123 * @param _First the first Object in the list 00124 * @return new OpenList from first and rest 00125 */ 00126 00127 public OpenList cons(Object _First) 00128 { 00129 return new OpenList(_First, this); 00130 } 00131 00132 00133 /** 00134 * Return the first of this non-empty OpenList. 00135 * 00136 * @return first of this list 00137 * @exception OpenListException if this list happens to be empty 00138 */ 00139 00140 public Object first() throws OpenListException 00141 { 00142 if( isEmpty() ) 00143 { 00144 throw new OpenListException(firstException); 00145 } 00146 return First; 00147 } 00148 00149 00150 /** 00151 * Return the first of the argument, a non-empty OpenList. 00152 * 00153 * @param List the list the rest of which is to be returned 00154 * @return first of the argument list 00155 * @exception OpenListException if the argument happens to be empty 00156 */ 00157 00158 public static Object first(OpenList List) throws OpenListException 00159 { 00160 return List.first(); 00161 } 00162 00163 00164 /** 00165 * Return the rest, i.e. all a list of all but the first of this non-empty 00166 * OpenList. 00167 * 00168 * @return rest of this list 00169 * @exception OpenListException if this list happens to be empty 00170 */ 00171 00172 public OpenList rest() throws OpenListException 00173 { 00174 if( isEmpty() ) 00175 { 00176 throw new OpenListException(restException); 00177 } 00178 return Rest; 00179 } 00180 00181 00182 /** 00183 * Return the rest, i.e. all a list of all but the first of the argument 00184 * non-empty OpenList. 00185 * 00186 * @parameter List the list the rest of which is to be returned 00187 * @return rest of the argument list 00188 * @exception OpenListException if the argument happens to be empty 00189 */ 00190 00191 public static OpenList rest(OpenList List) throws OpenListException 00192 { 00193 return List.rest(); 00194 } 00195 00196 00197 /** 00198 * Return true if this list is empty, false otherwise. 00199 * Note: Done by comparing this list to nil, which should be the <i>only</i> 00200 * way to determine whether the list is empty. 00201 * @return true if this list is empty, false otherwise 00202 */ 00203 00204 public boolean isEmpty() 00205 { 00206 return this == nil; 00207 } 00208 00209 00210 /** 00211 * Return true if this list is non-empty, false otherwise. 00212 * @return true if this list is non-empty, false otherwise 00213 */ 00214 00215 public boolean nonEmpty() 00216 { 00217 return this != nil; 00218 } 00219 00220 00221 /** 00222 * Return true if the argument list is empty, false otherwise. 00223 * Note: Done by comparing this list to nil, which should be the <i>only</i> 00224 * way to determine whether the list is empty. 00225 * 00226 * @param List the OpenList for which emptiness is to be determined 00227 * @return true if the argument list is empty, false otherwise 00228 */ 00229 00230 public static boolean isEmpty(OpenList List) 00231 { 00232 return List.isEmpty(); 00233 } 00234 00235 00236 /** 00237 * Return true if the argument list is non-empty, false otherwise. 00238 * 00239 * @param List the OpenList for which non-emptiness is to be determined 00240 * @return true if the argument list is non-empty, false otherwise 00241 */ 00242 00243 public static boolean nonEmpty(OpenList List) 00244 { 00245 return List.nonEmpty(); 00246 } 00247 00248 00249 /** 00250 * Create a new list formed by elements of the first argument followed by 00251 * those of the second. 00252 * 00253 * @param L1 the list providing the initial elements of the result 00254 * @param L2 the list providing the final elements of the result 00255 * @return the list containing the elements of the first list followed 00256 * by those of the second 00257 */ 00258 00259 public static OpenList append(OpenList L1, OpenList L2) 00260 { 00261 if( L1.isEmpty() ) 00262 { 00263 return L2; 00264 } 00265 else 00266 { 00267 return cons(L1.first(), append(L1.rest(), L2)); 00268 } 00269 } 00270 00271 00272 /** 00273 * Create a new list formed by elements of this list followed by those 00274 * of the argument list. 00275 * 00276 * @param L the list providing the final elements of the result 00277 * @return the list containing the elements of this list followed 00278 * by those of the second 00279 */ 00280 00281 public OpenList append(OpenList L2) 00282 { 00283 return append(this, L2); 00284 } 00285 00286 00287 /** 00288 * Return true if the first argument is a member (in the sense of being 00289 * equals) of the second argument list 00290 * 00291 * @param Ob the object being tested for membership 00292 * @param L the list in which membership is to be tested 00293 * @return true if the first argument is a member of the second, false 00294 * otherwise 00295 */ 00296 00297 public static boolean member(Object Ob, OpenList L) 00298 { 00299 for( ; L.nonEmpty(); L = L.rest() ) 00300 { 00301 if( Ob.equals(L.first()) ) return true; 00302 } 00303 00304 return false; 00305 } 00306 00307 00308 /** 00309 * Return true if the first argument is a member (in the sense of being 00310 * equals) of this OpenList 00311 * 00312 * @param Ob the object being tested for membership 00313 * @return true if the first argument is a member of this list, false 00314 * otherwise 00315 */ 00316 00317 public boolean member(Object Ob) 00318 { 00319 return member(Ob, this); 00320 } 00321 00322 00323 /** 00324 * Return a new list containing the elements of the argument list in 00325 * reverse order. 00326 * 00327 * @param L1 the list providing the elements 00328 * @return the reverse of the argument list 00329 */ 00330 00331 public static OpenList reverse(OpenList L1) 00332 { 00333 OpenList result = nil; 00334 00335 for( ; L1.nonEmpty(); L1 = L1.rest() ) 00336 { 00337 result = cons(L1.first(), result); 00338 } 00339 00340 return result; 00341 } 00342 00343 00344 /** 00345 * Return a new list containing the elements of the argument this list in 00346 * reverse order. 00347 * 00348 * @return the reverse of this list 00349 */ 00350 00351 public OpenList reverse() 00352 { 00353 return reverse(this); 00354 } 00355 00356 00357 /** 00358 * Return the number of elements of the argument list. 00359 * @param L the list, the length of which is to be determined 00360 * @return the number of elements of the argument list 00361 */ 00362 00363 public static int length(OpenList L) 00364 { 00365 int result = 0; 00366 00367 for( ; L.nonEmpty(); L = L.rest() ) 00368 { 00369 result++; 00370 } 00371 00372 return result; 00373 } 00374 00375 00376 00377 /** 00378 * Return the number of elements of this list. 00379 * @return the number of elements of this list 00380 */ 00381 00382 public int length() 00383 { 00384 return length(this); 00385 } 00386 00387 00388 00389 /** 00390 * Return the nth element of the second argument list, 00391 * starting with n = 0, 1, 2, ... 00392 * If there is no nth element, throws an OpenListException so indicating. 00393 * 00394 * @param n the index (0, 1, 2, ...) of the desired element. 00395 * @param L the list from which the indexed item is selected 00396 * @return the nth element of this list 00397 * @exception OpenListException if there is no nth element 00398 */ 00399 00400 public static Object nth(int n, OpenList L) throws OpenListException 00401 { 00402 int remaining = n; 00403 OpenList original = L; 00404 00405 try 00406 { 00407 while( remaining > 0 ) 00408 { 00409 L = L.rest(); 00410 remaining--; 00411 } 00412 return L.first(); 00413 } 00414 catch( OpenListException e ) 00415 { 00416 throw new OpenListException(nthException + original.length() + wherePart + n); 00417 } 00418 } 00419 00420 00421 /** 00422 * Return the nth element of this list, starting with n = 0, 1, 2, ... 00423 * If there is no nth element, throws an EmptyList exception so indicating. 00424 * 00425 * @param n the index (0, 1, 2, ...) of the desired element. 00426 * @return the nth element of this list 00427 * @exception OpenListException if there is no nth element 00428 */ 00429 00430 public Object nth(int n) throws OpenListException 00431 { 00432 return nth(n, this); 00433 } 00434 00435 00436 /** 00437 * Returns the first n elements of this OpenList. If there aren't n 00438 * elements in the list, returns the entire list. If n <= 0, returns nil. 00439 * 00440 * @param n the length of the desired prefix of this list. 00441 * @return the prefix of this list of the desired length, or the entire 00442 * list itself if the list is not as long as desired. 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 * Returns the first n elements of an OpenList. If there aren't n 00458 * elements in the list, returns the entire list. If n <= 0, returns nil. 00459 * 00460 * @param n the length of the desired prefix of this list. 00461 * @param L the list, the prefix of which is to be extracted 00462 * @return the prefix of this list of the desired length, or the entire 00463 * list itself if the list is not as long as desired. 00464 */ 00465 00466 static public OpenList prefix(int n, OpenList L) 00467 { 00468 return L.prefix(n); 00469 } 00470 00471 00472 /** 00473 * Return the second element of this OpenList, assuming it has one. 00474 * 00475 * @return the second element of this OpenList 00476 * @exception OpenListException if there is no second element 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 * Return the third element of this OpenList, assuming it has one. 00494 * 00495 * @return the third element of this OpenList 00496 * @exception OpenListException if there is no third element 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 * Return the second element of the argument OpenList, assuming it has one. 00514 * 00515 * @param L an OpenList, the second element of which is to be extracted 00516 * @return the second element L 00517 * @exception OpenListException if there is no second element 00518 */ 00519 00520 public static Object second(OpenList L) throws OpenListException 00521 { 00522 return L.second(); 00523 } 00524 00525 00526 /** 00527 * Return the third element of the argument OpenList, assuming it has one. 00528 * 00529 * @param L an OpenList, the third element of which is to be extracted 00530 * @return the second element L 00531 * @exception OpenListException if there is no third element 00532 */ 00533 00534 public static Object third(OpenList L) throws OpenListException 00535 { 00536 return L.third(); 00537 } 00538 00539 00540 /** 00541 * Return the list consisting of the argument objects, in this case the 00542 * empty list. 00543 * 00544 * @return the list consisting of the argument objects 00545 */ 00546 00547 public static OpenList list() 00548 { 00549 return nil; 00550 } 00551 00552 00553 /** 00554 * Return the list consisting of the argument objects. 00555 * 00556 * @param First the first element in the resulting list 00557 * @return the list consisting of the argument objects 00558 */ 00559 00560 public static OpenList list(Object First) 00561 { 00562 return cons(First, nil); 00563 } 00564 00565 00566 /** 00567 * Return the list consisting of the argument objects. 00568 * 00569 * @param First the first element in the resulting list 00570 * @param Second the second element in the resulting list 00571 * @return the list consisting of the argument objects 00572 */ 00573 00574 public static OpenList list(Object First, Object Second) 00575 { 00576 return cons(First, list(Second)); 00577 } 00578 00579 00580 00581 /** 00582 * Return the list consisting of the argument objects. 00583 * 00584 * @param First the first element in the resulting list 00585 * @param Second the second element in the resulting list 00586 * @param Third the third element in the resulting list 00587 * @return the list consisting of the argument objects 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 * Return the list consisting of the argument objects. 00598 * 00599 * @param First the first element in the resulting list 00600 * @param Second the second element in the resulting list 00601 * @param Third the third element in the resulting list 00602 * @param Fourth the fourth element in the resulting list 00603 * @return the list consisting of the argument objects 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 * Return the list consisting of the argument objects. 00617 * 00618 * @param First the first element in the resulting list 00619 * @param Second the second element in the resulting list 00620 * @param Third the third element in the resulting list 00621 * @param Fourth the fourth element in the resulting list 00622 * @param Fifth the fifth element in the resulting list 00623 * @return the list consisting of the argument objects 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 * Return the list consisting of the argument objects. 00638 * 00639 * @param First the first element in the resulting list 00640 * @param Second the second element in the resulting list 00641 * @param Third the third element in the resulting list 00642 * @param Fourth the fourth element in the resulting list 00643 * @param Fifth the fifth element in the resulting list 00644 * @param Sixth the sixth element in the resulting list 00645 * @return the list consisting of the argument objects 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 * Return the list consisting of the argument objects. 00661 * 00662 * @param First the first element in the resulting list 00663 * @param Second the second element in the resulting list 00664 * @param Third the third element in the resulting list 00665 * @param Fourth the fourth element in the resulting list 00666 * @param Fifth the fifth element in the resulting list 00667 * @param Sixth the sixth element in the resulting list 00668 * @param Seventh the seventh element in the resulting list 00669 * @return the list consisting of the argument objects 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 * Return true if the two OpenLists are equal, in the sense of having 00686 * the equal elements in both listss. 00687 * 00688 * @param L1 one of two OpenLists to be compared for equality 00689 * @param L2 the other of two OpenLists to be compared for equality 00690 * @return true if the arguments are equal, false otherwise 00691 */ 00692 00693 public static boolean equals(OpenList L1, OpenList L2) 00694 { 00695 // As long as both lists have elements, check for the equality of 00696 // those elements. 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 // Now at least one of the lists is empty. 00710 // The two are equal if, and only if, both are empty. 00711 00712 return L1.isEmpty() && L2.isEmpty(); 00713 } 00714 00715 00716 /** 00717 * Return true if this list is equal to the argument list. 00718 * 00719 * @param Ob the Object to be compared with this for equality 00720 * @return true if the argument is an OpenList equal to this one. 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; // An OpenList can only be equals to another OpenList 00730 } 00731 00732 00733 /** 00734 * Convert this OpenList to a String, using default punctuation. 00735 * 00736 * @return String representing this OpenList 00737 */ 00738 00739 public String toString() 00740 { 00741 return toString(defaultLeftParen, defaultSpacer, defaultRightParen); 00742 } 00743 00744 00745 /** 00746 * Convert this OpenList to a String, using the arguments as punctuation. 00747 * 00748 * @param leftParen String that is output before the list elements 00749 * @param spacer String that is output between list elements 00750 * @param rightParen String that is output after the list elements 00751 * @return String representing OpenList 00752 */ 00753 00754 public String toString(String leftParen, 00755 String spacer, 00756 String rightParen) 00757 { 00758 StringBuffer buffer = new StringBuffer(); 00759 00760 buffer.append(leftParen); 00761 00762 if( nonEmpty() ) 00763 { 00764 buffer.append(first().toString()); 00765 OpenList remainder = rest(); 00766 00767 while( remainder.nonEmpty() ) 00768 { 00769 buffer.append(spacer); 00770 buffer.append(remainder.first()); 00771 remainder = remainder.rest(); 00772 } 00773 } 00774 00775 buffer.append(rightParen); 00776 return buffer.toString(); 00777 } 00778 00779 00780 /** 00781 * Explode the argument String into a list of Characters wrapping 00782 * the characters of the String. 00783 * 00784 * @param String to be exploded 00785 * @return OpenList containing the characters of the String 00786 */ 00787 00788 public static OpenList explode(String s) 00789 { 00790 OpenList result = nil; 00791 00792 for( int i = s.length()-1; i >= 0; i-- ) 00793 { 00794 result = cons(new Character(s.charAt(i)), result); 00795 } 00796 00797 return result; 00798 } 00799 00800 00801 /** 00802 * Implode the contents of an OpenList into a single String. 00803 * The list can contain any Objects, not just Characters. 00804 * The toString method is used to get the character representation of 00805 * the object. No parentheses or spacing is added around or between 00806 * the elements of the outer list. If these are wanted, use the 00807 * toString method rather than implode. 00808 * 00809 * @param L the list to be imploded 00810 * @return String representing the imploded list. 00811 */ 00812 00813 public static String implode(OpenList L) 00814 { 00815 StringBuffer buffer = new StringBuffer(); 00816 00817 for( ; L.nonEmpty(); L = L.rest() ) 00818 { 00819 buffer.append(L.first().toString()); 00820 } 00821 00822 return buffer.toString(); 00823 } 00824 00825 00826 /** 00827 * Implode this OpenList into a single String. 00828 * The list can contain any Objects, not just Characters. 00829 * The toString method is used to get the character representation of 00830 * the object. No parentheses or spacing is added around or between 00831 * the elements of the outer list. 00832 * 00833 * @return String representing the imploded list. 00834 */ 00835 00836 public String implode() 00837 { 00838 return implode(this); 00839 } 00840 00841 00842 /** 00843 * Return an array of the Objects in the argument OpenList. 00844 * 00845 * @param L the list of objects to be used as elements of the array 00846 * @return an array of the Objects in the argument OpenList 00847 */ 00848 00849 public static Object[] toArray(OpenList L) 00850 { 00851 Object a[] = new Object[L.length()]; 00852 00853 int i = 0; 00854 00855 for( ; L.nonEmpty(); L = L.rest() ) 00856 { 00857 a[i] = L.first(); 00858 i++; 00859 } 00860 return a; 00861 } 00862 00863 00864 /** 00865 * Return an array of the Objects in this OpenList. 00866 * 00867 * @return an array of the Objects in this OpenList 00868 */ 00869 00870 public Object[] toArray() 00871 { 00872 return toArray(this); 00873 } 00874 00875 00876 /** 00877 * Create an OpenList from an array of Objects. 00878 * 00879 * @return an OpenList containing the Objects in the argument array 00880 */ 00881 00882 public static OpenList fromArray(Object a[]) 00883 { 00884 OpenList result = nil; 00885 00886 for( int i = a.length-1; i >= 0; i-- ) 00887 { 00888 result = cons(a[i], result); 00889 } 00890 00891 return result; 00892 } 00893 00894 00895 /** 00896 * Return a sorted version of this list, as determined by the Comparator, 00897 * using the Quicksort algorithm 00898 * 00899 * @param comparator used to determine whether one element is less than 00900 * another 00901 * @return the list consisting of the Objects in the original list 00902 * in order as determined by the comparator 00903 */ 00904 00905 public OpenList sort(java.util.Comparator comparator) 00906 { 00907 Object a[] = this.toArray(); 00908 00909 Quicksort q = new Quicksort(a); 00910 00911 q.sort(comparator); 00912 00913 return fromArray(a); 00914 } 00915 00916 00917 /** 00918 * Read lines from a BufferedReader, each as a separate String. 00919 * The lines read are returned as an OpenList of Strings. 00920 * 00921 *@param reader reads the lines that are compiled into a list 00922 *@return list of Strings read, one String per line 00923 */ 00924 00925 static OpenList readLines(BufferedReader reader) throws IOException 00926 { 00927 String stringRead = reader.readLine(); 00928 00929 if( stringRead == null ) 00930 return nil; 00931 00932 return cons(stringRead, readLines(reader)); 00933 } 00934 00935 00936 /** 00937 * test program: 00938 * exercises a variety of the defined methods, printing out results 00939 */ 00940 00941 public static void main(String arg[]) 00942 { 00943 try 00944 { 00945 OpenList L0 = nil; 00946 00947 System.out.println("L0 = " + L0); 00948 00949 OpenList L1 = cons("a", cons("b", cons("c", nil))); 00950 00951 System.out.println("L1 = " + L1); 00952 00953 OpenList L2 = cons(cons("b", cons("c", nil)), cons("d", nil)); 00954 00955 System.out.println("L2 = " + L2); 00956 00957 OpenList L3 = cons(L1, cons(L2, nil)); 00958 00959 System.out.println("L3 = " + L3); 00960 00961 OpenList L4 = cons(L1, L2); 00962 00963 System.out.println("L4 = " + L4); 00964 00965 OpenList L5 = cons(L2, L1); 00966 00967 System.out.println("L5 = " + L5); 00968 00969 OpenList L6 = cons(new Integer(1), nil); 00970 00971 System.out.println("L6 = " + L6); 00972 00973 OpenList L7 = cons(new Integer(1), 00974 cons(new Integer(2), 00975 cons(new Integer(3), 00976 cons(new Integer(4), 00977 nil)))); 00978 00979 System.out.println("L7 = " + L7); 00980 00981 System.out.println("append(L1, L2) = " + append(L1, L2)); 00982 00983 System.out.println("reverse(L5) = " + reverse(L5)); 00984 00985 System.out.println("append(reverse(L7), reverse(L1)) = " 00986 + append(reverse(L7), reverse(L1))); 00987 00988 System.out.println("reverse(append(L1, L7)) = " 00989 + reverse(append(L1, L7))); 00990 00991 System.out.println("L5.length() = " + L5.length()); 00992 00993 System.out.println("L5.nth(2) = " + L5.nth(2)); 00994 00995 System.out.println("L5.prefix(3) = " + L5.prefix(3)); 00996 00997 System.out.println("explode(\"Hello, World\") = " 00998 + explode("Hello, World")); 00999 01000 System.out.println("implode(explode(\"Hello, World\")) = " 01001 + implode(explode("Hello, World"))); 01002 01003 System.out.println("equals(append(reverse(L1), reverse(L2)), " 01004 + " reverse(append(L2, L1))) = " 01005 + equals(append(reverse(L1), reverse(L2)), 01006 reverse(append(L2, L1)))); 01007 01008 System.out.println("L7.equals(reverse(reverse(L7))) = " 01009 + L7.equals(reverse(reverse(L7)))); 01010 01011 // This will intentionally throw an exception. 01012 01013 System.out.println("L5.nth(4) = " + L5.nth(4)); 01014 01015 } 01016 catch( Exception e ) 01017 { 01018 System.err.println("*** caught: " + e); 01019 } 01020 } 01021 } 01022 01023 /* Output from main: 01024 01025 L0 = [] 01026 L1 = [a, b, c] 01027 L2 = [[b, c], d] 01028 L3 = [[a, b, c], [[b, c], d]] 01029 L4 = [[a, b, c], [b, c], d] 01030 L5 = [[[b, c], d], a, b, c] 01031 L6 = [1] 01032 L7 = [1, 2, 3, 4] 01033 append(L1, L2) = [a, b, c, [b, c], d] 01034 reverse(L5) = [c, b, a, [[b, c], d]] 01035 append(reverse(L7), reverse(L1)) = [4, 3, 2, 1, c, b, a] 01036 reverse(append(L1, L7)) = [4, 3, 2, 1, c, b, a] 01037 L5.length() = 4 01038 L5.nth(2) = b 01039 L5.prefix(3) = [[[b, c], d], a, b] 01040 explode("Hello, World") = [H, e, l, l, o, ,, , W, o, r, l, d] 01041 implode(explode("Hello, World")) = Hello, World 01042 equals(append(reverse(L1), reverse(L2)), reverse(append(L2, L1))) = true 01043 L7.equals(reverse(reverse(L7))) = true 01044 *** caught: OpenListException: nth of an OpenList of length 4, where n == 4 01045 01046 */
1.2.6 written by Dimitri van Heesch,
© 1997-2001