Lists

Definition

With the power of conses, one can link them together to make a true list. This is the same as a linked list data structure in Java. Essentially each node contains a data element and a pointer to another node. The car of each cons points to one of the elements of the list, and the cdr points either to another cons or to nil for as long as you want your list to be. Here it is verified in Jatha.

Common LISP Code

(cons 4 9)
(cons 3 (cons 2 8))
(cons 7 (cons 1 (cons 3 11)))
(cons 2 (cons 1 (cons 6 (cons 3 7))))

Common LISP Output

(4 . 9)
(3 2 . 8)
(7 1 3 . 11)
(2 1 6 3 . 7)

Java Code

package jathaverify;
import org.jatha.*;
import org.jatha.dynatype.*;
import java.util.*;
public class Main  {

    public Main() {
    }

    public static void main(String[] args) {
        Jatha myLisp  = new Jatha(false, false);
        myLisp.init();
        myLisp.start();

        try {
            List lisp = new ArrayList();
            lisp.add("(cons 8 9)");
            lisp.add("(cons (cons 4 2) 1)");
            lisp.add("(cdr (cons 2 1))");
            lisp.add("(car (cons 6 1))");

            Iterator i = lisp.iterator();
            while (i.hasNext()) {
                String query = i.next().toString();
                System.out.println(query + " = " + myLisp.eval(query));
             }

        } catch (Exception e) {
            System.err.println("LISP Exception: " + e);
        }

    }
}

Java Output (Jatha)

compile:
run:
(cons 4 9) = (4 . 9)
(cons 3 (cons 2 8)) = (3 2 . 8)
(cons 7 (cons 1 (cons 3 11))) = (7 1 3 . 11)
(cons 2 (cons 1 (cons 6 (cons 3 7)))) = (2 1 6 3 . 7)
BUILD SUCCESSFUL (total time: 1 second)

Result

Rock!