Contents    Page-10    Prev    Next    Page+10    Index   

Access to Parts of a List

The two fields of a cons cell are traditionally called car and cdr, but perhaps better names are first, the first item in the list, and rest, the link to the rest of the list.


(first '(a b c))            ->  a
first(list("a", "b", "c"))

(rest '(a b c))             ->  (b c)
rest(list("a", "b", "c"))


public static Object first(Cons lst) {
    return ( (lst == null) ? null : lst.car  ); }

public static Cons rest(Cons lst) {
    return ( (lst == null) ? null : lst.cdr  ); }

Note that first and rest in general have different types; first can be any Object type, while rest is a Cons or null.

The functions second and third are also defined.