|Third Assignment: Implementing
Dave Matuszek, Spring 2003
Implement the fundamental Lisp functions, plus a few others.
Implement the following three classes and their methods. You may define additional
classes and methods as needed; however, consider whether additional methods
public static final Sexpr NIL
public static final Sexpr T
public static final Sexpr QUOTEMethods:
public Sexpr(String value)
- Creates and returns an atom representing an uppercase version of the given String.
public Sexpr(int value)
- Creates and returns an atom representing the given numeric value. Depending on your implementation, you may find the
java.lang.Integerwrapper class helpful.
public Sexpr(Sexpr arg1, Sexpr arg2)
- Creates and returns a new list whose
public String toString()
- Returns a String representation of this Sexpr.
Note: This method is most simply written as a recursive method.
public static Sexpr parse(String arg)
- Given an argument that represents either an atom or a list, return the S-expression corresponding to that list. If there is an error in the argument, print an error message and return
Note 1: This is probably the most difficult method in the entire assignment;
java.util.StringTokenizeris your friend.
Note 2: You do not need to parse the special syntax for quoting an S-expression with the
'symbol, and you do not need to parse dotted pairs. Just atoms (variable names and integers) and lists.
Note 3: This method is most simply written as a recursive method.
- None required.
static public Sexpr quote(Sexpr arg)
- Implements Lisp QUOTE: That is, it returns its argument unchanged.
static public Sexpr car(Sexpr arg)
- Implements Lisp CAR.
static public Sexpr cdr(Sexpr arg)
- Implements Lisp CDR.
static public Sexpr cons(Sexpr arg1, Sexpr arg2)
- Implements Lisp CONS.
static public Sexpr eq(Sexpr arg1, Sexpr arg2)
- Implements Lisp EQ (see Note below).
static public Sexpr atom(Sexpr arg)
- Implements Lisp ATOM (see Note below).
static public Sexpr null(Sexpr arg)
- Implements Lisp NULL (see Note below).
static public Sexpr numberp(Sexpr arg)
- Implements Lisp NUMBERP, which returns "true" if its argument is a number (see Note below). The only numbers in this assignment will be integers.
static public Sexpr equal(Sexpr arg1, Sexpr arg2)
- Implements Lisp EQUAL (see Note below). This method is most simply written as a recursive method.
Note: As defined above, Lisp predicates
atom, null, equal, numberpdo not return a
booleanvalue; instead, they return a value of either
Sexpr.T(or, for a "true" result, possibly something else).
- As needed.
public static void main(String args)
- Thoroughly tests all the above methods. Part of your grade may be based on how thorough you make your test cases.
Lisp can be implemented as ADTs. Think
about what data and operations should be made available to the user of the class.
Note that the only user of
Sexpr should be the
class, so this gives you some additional flexibility in designing
but you should still work to make it an ADT. The
Lisp class, on
the other hand, may (theoretically) have a much wider audience, so you need
to be careful not to expose any more of its inner workings than absolutely necessary.
Methods, etc., that should be available to the user should be marked
Everything else should be
All classes, constructors, variables, and methods should either be
or should be fully documented with
comments. Things which are marked
private, if they are reasonably
self-explanatory, do not need to be documented.
Follow, as closely as possible, the implementation of Lisp as described in
class. Don't try to use
Lists from Java's
Due date: Thursday, February 6, before midnight.