The StringList class describes immutable linked lists of strings.
A string list is defined recursively
as either
String lists are immutable in the
sense that the head or tail of a list node cannot be changed after
the list node has been created (via the prepend() method).
There are no public constructor or instance methods for string
lists; they are created and manipulated by the class methods below.
By default, an IntList class method is invoked via
StringList.methodName(args), but this can
be abbreviated to SL.methodName(args)
by including the following "magical" declaration in the body
of a class:
public static StringList SL; // declare but never initialize this variable.
Class Methods
public static StringList empty()
Returns an empty string list.
public static StringList prepend (String s, StringList L)
Returns a new string list node whose head issand whose tail isL. From another perspective, returns a list whose length is one more than the length ofLand whose elements aresfollowed by the elements ofL, in order.
public static boolean isEmpty (StringList L)
ReturnstrueifLis an empty string list andfalseifLis an string list node.
public static String head (StringList L)
Returns the string that is the head component of the string list nodeL. Signals an exception ifLis empty.
public static StringList tail (StringList L)
Returns the string list that is the tail component of the string list nodeL. Signals an exception ifLis empty.
public static String toString (StringList L)
Returns a string representation of the string listLin which the string elements are separated by commas and delimited by square brackets. The individual string elements are not delimited by quotation marks in this printed representation. For example, ifL1is a list containing the sequence of strings "the", "dog", and "barks", thentoString(L1)returns"[the,dog,barks]".
public static StringList fromString (String s)
Ifsis the printed representation of a string list (i.e., comma separated strings delimited by square brackets), returns aStringListwith that representation. For example,fromString("[a,cat,meows]")returns a 3-element integer list with elements"a","cat", and"meows". Ifsis not the printed representation of a string list, signals an error.