import java.io.*; class CharListDIO { public static void print(CharList L) { System.out.print("["); if(!L.isempty()) { System.out.print(L.head()); printRestList(L.tail()); } System.out.print("]"); } private static void printRestList(CharList L) { if(!L.isempty()) { System.out.print(","+L.head()); printRestList(L.tail()); } } public static CharList read(BufferedReader in) throws IOException,ListFormatException { String excmess,line = in.readLine(); try { if(line.charAt(0)!='[') { excmess="Expected '[', found '"+line.charAt(0)+"'"; throw new ListFormatException(excmess); } else if(line.charAt(1)==']') return CharListD.empty(); else return readRestList(line,2).cons(line.charAt(1)); } catch(StringIndexOutOfBoundsException e) { excmess="Unexpected end of line"; throw new ListFormatException(excmess); } } private static CharList readRestList(String line,int i) throws ListFormatException { String excmess; if(line.charAt(i)==']') { if(++i!=line.length()) { excmess="Extra characters: \""; excmess+=line.substring(i,line.length())+"\""; throw new ListFormatException(excmess); } return CharListD.empty(); } else if(line.charAt(i)!=',') { excmess="Expected ']' or ','"; excmess+=", found '"+line.charAt(i)+"'"; throw new ListFormatException(excmess); } else return readRestList(line,i+2).cons(line.charAt(i+1)); } }