/* ObjectList ::= EmptyObjectList | ConsObjectList where ConsObjectList includes an Object and an ObjectList. */ abstract class ObjectList { /** Adds the element n to the front of this ObjectList. */ ObjectList cons(Object n) { return new ConsObjectList(n, this); } /** Returns a String representation of this ObjectList in Scheme format: (e1 ... en) . */ abstract String listString(); /** Returns s String containing the elements of this ObjectList, where each element is preceded by ' '. Hence, * (e1 e2... en) generates " e1 e2 ... en" */ abstract String listStringHelp(); } class EmptyObjectList extends ObjectList { /** Field containing the only (singleton) EmptyObjectList. */ static EmptyObjectList ONLY = new EmptyObjectList(); /** Private constructor used to construct a singleton. */ private EmptyObjectList() { } String listString() { return "()"; } String listStringHelp() { return ""; } } class ConsObjectList extends ObjectList { /** The first element of this ObjectList. */ Object first; /** The remaining elements of this ObjectList. */ ObjectList rest; String listString() { return "(" + first + rest.listStringHelp() + ")"; } String listStringHelp() { return " " + first + rest.listStringHelp(); } }