Node: List operations, Next: , Previous: Conses, Up: Lists



List operations

The following are operations on lists.

nth n list Function

This returns the nth element indicated by n of the list list where the car of list is the 0th element. n may be an Lpp Integer or an int and must be positive. If the length of the list is not greater than n then nil is returned. For example:

     let list1 = list(S(zero), S(one), S(two));
     nth(0, list1) => zero
     nth(L(2), list1) => two
     nth(10, list1) => nil
     

nthcdr n list Function

This returns the result of applying the cdr function n times on list. n may be an Lpp Integer or an int and must be positive. If the length of the list is not greater than n then nil is returned. For example:

     let list1 = list(S(zero), S(one), S(two));
     nthcdr(1, list1) => (one two)
     nthcdr(10, list1) => nil
     

first list Function

second list Function

third list Function

fourth list Function

fifth list Function

sixth list Function

seventh list Function

eighth list Function

ninth list Function

tenth list Function

These functions are provided for convenient accessing of predetermined list element positions instead of nth and are more efficient. first returns the same as nth with a first argument of 0, second with a first argument of 1, etc.

list args Function

list constructs and returns a list of args which can be 1 or more arguments of type let. Up to 10 arguments may be given. For example:

     list(L("zero"), S(one), L(2)) => ("zero" one 2)
     list(S(zero), S(one), list(L(2), L(3)))
             => (zero one (2 3))
     

If more than 10 is needed listEM should be used.

EM Constant

listEM args Function

listEM is like list except that args can be any number of type let arguments. A global constant EM, abbreviation for "End Marker", terminates the arguments. For example:

     listEM(L("zero"), S(one), L(2), EM) => ("zero" one 2)
     let n = L(1);
     listEM(n, n, n, n, n, n, n, n, n, n, n, EM)
             => (1 1 1 1 1 1 1 1 1 1 1)
     

listSEM args Function

listSEM is like listEM except that args can be any number of type char* arguments. For example:

     listSEM("zero", "one", "two", EM) => ("zero" "one" "two")
     

last list Function

last list n Function

last returns the last cons of the list list. With the optional integer argument n it returns the tail of the list consisting of the last n conses of list. The n argument may be either an Lpp Integer or an int.

endp object Function

endp is false if object is a cons, true if nil, and signals an error for all other values. It is useful for checking for the end of a proper list.

listLength list Function

listLengthL list Function

listLength is the same as length applied to a list but is guaranteed to return -1 if the list list is a circular list. listLengthL is the same as lengthL but returns nil if list is a circular list. listLengthL can also be used as a function object.

copyList list Function

copyList returns a list that is equal to list but not eq. Only the top level of the list is copied, that is it copies in the cdr direction and not the car direction.

copyAlist list Function

copyAlist is meant for copying association lists, See Association Lists. It is just like copyList except that for each element a cons is replaced in the copy by a new cons with the same car and cdr.

copyTree object Function

copyTree is for copying trees of conses. If object is not a cons it is just returned. If object is a cons the whole tree of conses in its car and cdr is copied recursively and returned. In this process conses are copied but non-conses are placed in the new cons as is.

nconc list1 list2 Function

nconc concatenates list1 to list2 and return the concatenated list. Its arguments must be lists or nil. The arguments are changed rather than copied.

listConcat list1 list2 Function

listConcat is just like nconc except that its arguments list1 and list2 are copied and the copies are concatenated and returned.

For the next two macros push and pop the argument place is any variable that contains a list. Note that by the definition of a list nil is also considered a list.

push item place Macro

item is any object of type let. After the push macro executes place will be a list with item as the car of the list and the list that was in place as the cdr.

pop var place Macro

var is any variable of type let. After the pop macro executes var will contain the car of place and place will contain the cdr.