List processing

LISP functions working with lists.

CONS

Expects two arguments, creates and returns new cons cell with first argument in CAR second in CDR.

>(cons 'a nil)
(A)            ; proper list with single element
 
>(cons 'a 'b)
(A . B)        ; cons cell, not proper list

LIST

Expect zero or more arguments, returns the arguments as proper list.

>(list 'a 'b 'c)
(A B C)            ; proper list with three elements
 
>(cons 'a (list 'b 'c 'd))
(A B C D)          ; we can cons to list 

APPEND

Expect zero or more arguments, and returns them in single proper list. The arguments that are lists will be connected together. Non list arguments will be added as elements. The returned list is freshly consed, so the original lists are not affected. In LabLISP, APPEND is extremely benevolent and accepts dotted or even cyclic lists. When the new cons cell are created, the trailing atoms in dotted lists are ignored, and cyclic list is cut at the cell which has a CDR pointing to some previous element of the list.

>(setq l1 '(a b c))
(A B C)
>(setq l2 '(e f))
(E F)
>(append l1 'd l2)
(A B C D E F)   
>l1       
(A B C)            ; original L1 is untouched

NCONC

Evil twin of APPEND. Expect zero or more arguments, and returns them in single list. List arguments will be now merged to the final list, so the original lists are affected. For dotted lists, the trailing atom is dropped, unless it is at the end of last list argument. In that case the returned list will also be dotted. Cyclic lists are not allowed in the middle of the arguments, but the last list can be cyclic, and so will be the returned list.

>(setq l1 '(a b c))       ; propper list
(A B C)
>(setq l2 (cons 'e 'f))   ; this cons is a dotted list
(E . F)
>(nconc l1 l2)
(A B C E . F)  
>l1       
(A B C E . F)             ; original L1 is modified 

LENGTH

Expects one argument that must be proper list, returns the length.

>(length (list 'a 'b 'c)) ; proper list with three elements
3           
 
>(length nil)             ; NIL is (), i.e. empty list
0             

CAR CDR etc. (Accessor)

Functions accessing list elements. They expect single argument that must be list. CAR, CADR, CADDR, CADDDR access first, second, third and fourth element of list. With combinations of As and Ds can access elements of cons cell trees, or lists of lists: CAAR gets first element of first sublist, and so on, with all combinations up to CAAAAR and CDDDDR.

>(car '(a b c))
A 
 
>(cdr '(a b c))
(B C)
 
>(cadr '(a b c))
B
 
>(caddr '(a b c))
C         
 
>(cadaar '(((a b) c d) e f))
B
 
>(cdddr '(a b c d e f))
(D E F)

FIRST to TENTH (Accessor)

Equivalents to CAR, CADR, CADDR, CADDDR, and above, up to CAR of tenth cons cell.

>(second '(a b c))
B       

REST (Accessor)

Is the same as CDR. That means FIRST and REST are synonymes to CAR and CDR, respectively.

NTH NTHCDR (Accessor)

Another equivalent access functions to list elements. Expects two arguments, first is integer index, starting from 0, second is the list.

>(nth 0 '(a b c))
A
 
>(nth 2 '(a b c))
C
 
>(nthcdr 0 '(a b c))
(A B C)       

LAST

Returns last N cons cells of a list. Expects 2 arguments, first must be list, second argument is the count N. If second argument is missing, returns the last one cons cell.

>(last (list 'a 'b 'c 'd 'e) 2) 
(D E)                      ; last 2 cons cells