/** IntList ::= EmptyIntList | ConsIntList(int, IntList). */ abstract class IntList { /** Sorts this IntList into ascending (non-descending) order. */ abstract IntList sort(); /** Adds the int n to the front of this IntList. */ IntList cons(int n) { return new ConsIntList(n, this); } /** Inserts n in proper order in this, assuming this is sorted in ascending order. */ abstract IntList insert(int n); } class EmptyIntList extends IntList { IntList sort() { return this; } IntList insert(int n) { return cons(n); } } class ConsIntList extends IntList { /** The first element of this IntList. */ int first; /** The remaining elements of this IntList. */ IntList rest; IntList sort() { return rest.sort().insert(first); } IntList insert(int n) { if (n <= first) return cons(n); else return rest.insert(n).cons(first); } }