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


7.4 Cutting and Pasting Lists

SRFI 1 procedure: take x i
SRFI 1 procedure: drop x i

take returns the first i elements of list x. drop returns all but the first i elements of list x.

(take '(a b c d e)  2) => (a b)
(drop '(a b c d e)  2) => (c d e)

x may be any value—a proper, circular, or dotted list:

(take '(1 2 3 . d) 2) => (1 2)
(drop '(1 2 3 . d) 2) => (3 . d)
(take '(1 2 3 . d) 3) => (1 2 3)
(drop '(1 2 3 . d) 3) => d

For a legal i, take and drop partition the list in a manner which can be inverted with append:

(append (take x i) (drop x i)) = x

drop is exactly equivalent to performing i cdr operations on x; the returned value shares a common tail with x. If the argument is a list of non-zero length, take is guaranteed to return a freshly-allocated list, even in the case where the entire list is taken, e.g. (take lis (length lis)).

obsolete procedure: list-head x i
standard procedure: list-tail x i

Equivalent to take and drop, respectively. list-head is deprecated and should not be used. list-tail is defined by R7RS.

procedure: sublist list start end

Start and end must be exact integers satisfying

0 <= start <= end <= (length list)

sublist returns a newly allocated list formed from the elements of list beginning at index start (inclusive) and ending at end (exclusive).

standard procedure: append list …

Returns a list consisting of the elements of the first list followed by the elements of the other lists.

(append '(x) '(y))                      ⇒ (x y)
(append '(a) '(b c d))                  ⇒ (a b c d)
(append '(a (b)) '((c)))                ⇒ (a (b) (c))
(append)                                ⇒ ()

The resulting list is always newly allocated, except that it shares structure with the last list argument. The last argument may actually be any object; an improper list results if the last argument is not a proper list.

(append '(a b) '(c . d))                ⇒ (a b c . d)
(append '() 'a)                         ⇒ a
SRFI 1 procedure: append! list …

Returns a list that is the argument lists concatenated together. The arguments are changed rather than copied. (Compare this with append, which copies arguments rather than destroying them.) For example:

(define x (list 'a 'b 'c))
(define y (list 'd 'e 'f))
(define z (list 'g 'h))
(append! x y z)                         ⇒ (a b c d e f g h)
x                                       ⇒ (a b c d e f g h)
y                                       ⇒ (d e f g h)
z                                       ⇒ (g h)
SRFI 1 procedure: last pair
SRFI 1 procedure: last-pair pair

last returns the last element of the non-empty, finite list pair. last-pair returns the last pair in the non-empty, finite list pair.

(last '(a b c)) => c
(last-pair '(a b c)) => (c)
obsolete procedure: except-last-pair list
obsolete procedure: except-last-pair! list

These procedures are deprecated. Instead use drop-right or drop-right!, respectively, with a second argument of 1.


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