Next: , Previous: , Up: Lists   [Contents][Index] Accessing List Elements

The most flexible method of accessing an element in a list is via a combination of the car and cdr functions. There are other functions which provide an easier way to get at the elements in a flat list. These will usually be faster than a string of car and cdr operations.

Function: nth count list

This function returns the element count elements down the list, therefore to access the first element use a count of zero (or even better the car function). If there are too few elements in the list and no element number count can be found () is returned.

(nth 3 '(0 1 2 3 4 5))
    ⇒ 3

(nth 0 '(foo bar)
    ⇒ foo
Function: nthcdr count list

This function takes the cdr of the list list count times, returning the last cdr taken.

(nthcdr 3 '(0 1 2 3 4 5))
    ⇒ (3 4 5)

(nthcdr 0 '(foo bar))
    ⇒ (foo bar)
Function: last list

This function returns the last element in the list list. If the list has zero elements () is returned.

(last '(1 2 3))
    ⇒ 3

(last '())
    ⇒ ()
Function: member object list

This function scans through the list list until it finds an element which is equal to object. The tail of the list (the cons cell whose car is the matched object) is then returned. If no elements match object then the empty list () is returned.

(member 'c '(a b c d e))
    ⇒ (c d e)

(member 20 '(1 2))
    ⇒ ()
Function: memq object list

This function is similar to member except that comparisons are performed by the eq function not equal.

Next: , Previous: , Up: Lists   [Contents][Index]