Lisp for Java Users: Lisp List

This little program gives us something simple to work with. One obvious problem is that it reads lists using Lisp notation, enclosed by parentheses and with spaces between entries, but prints them in Java notation surrounded by square brackets and using commas between entries. A simple extension to the ArrayList class allows us to fix that. This simple class overrides the toString method to make the output look like Lisp.

public class LispList extends ArrayList<Object> { private static final char OPEN_PAREN = '('; private static final char CLOSE_PAREN = ')'; @Override public String toString () { final StringBuilder buffer = new StringBuilder (); buffer.append (OPEN_PAREN); for (int i = 0; i < size (); i++) { if (i > 0) { buffer.append (" "); } buffer.append (get (i)); } buffer.append (CLOSE_PAREN); return buffer.toString (); } }

We need to make a one line change in the read method to use this class:

final List<Object> result = new LispList ();

This version of the ReadDemo program includes these changes.