Next: srfi list spec fold, Previous: srfi list spec select, Up: srfi list spec [Index]
Both length
and length+
return the length of the argument.
It is an error to pass a value to length
which is not a proper
list (finite and nil–terminated). In particular, this means an
implementation may diverge or signal an error when length
is
applied to a circular list.
length+
, on the other hand, returns #f
when applied to a
circular list.
The length of a proper list is a non–negative integer n such that
cdr
applied n times to the list produces the empty list.
R5RS append
returns a list consisting of the elements of
list1 followed by the elements of the other list parameters.
(append '(x) '(y)) => (x y) (append '(a) '(b c d)) => (a b c d) (append '(a (b)) '((c))) => (a (b) (c))
The resulting list is always newly allocated, except that it shares structure with the final listi argument. This last argument may be any value at all; an improper list results if it is not a proper list. All other arguments must be proper lists.
(append '(a b) '(c . d)) => (a b c . d) (append '() 'a) => a (append '(x y)) => (x y) (append) => ()
append!
is the linear–update variant of append
; it is
allowed, but not required, to alter cons cells in the argument lists to
construct the result list. The last argument is never altered; the
result list shares structure with this parameter.
These functions append the elements of their argument together. That
is, concatenate
returns:
(apply append list-of-lists)
or, equivalently:
(reduce-right append '() list-of-lists)
concatenate!
is the linear–update variant, defined in terms
of append!
instead of append
.
Note that some Scheme implementations do not support passing more than a
certain number (e.g. 64) of arguments to an n–ary procedure. In these
implementations, the (apply append ...)
idiom would fail when
applied to long lists, but concatenate would continue to function
properly.
As with append
and append!
, the last element of the input
list may be any value at all.
R5RS reverse
returns a newly allocated list consisting of the
elements of list in reverse order.
(reverse '(a b c)) => (c b a) (reverse '(a (b c) d (e (f)))) => ((e (f)) d (b c) a)
reverse!
is the linear–update variant of reverse
. It
is permitted, but not required, to alter the argument’s cons cells to
produce the reversed list.
append-reverse
returns:
(append (reverse rev-head) tail)
It is provided because it is a common operation, a common list–processing style calls for this exact operation to transfer values accumulated in reverse order onto the front of another list, and because the implementation is significantly more efficient than the simple composition it replaces.
But note that this pattern of iterative computation followed by a
reverse
can frequently be rewritten as a recursion, dispensing with thereverse
andappend-reverse
steps, and shifting temporary, intermediate storage from the heap to the stack, which is typically a win for reasons of cache locality and eager storage reclamation.
append-reverse!
is just the linear–update variant; it is
allowed, but not required, to alter rev-head’s cons cells to
construct the result.
Defined as:
(lambda lists (apply map list lists))
If zip
is passed n lists, it returns a list as long as the
shortest of these lists, each element of which is an n–element
list comprised of the corresponding elements from the parameter lists.
(zip '(one two three) '(1 2 3) '(odd even odd even odd even odd even)) => ((one 1 odd) (two 2 even) (three 3 odd)) (zip '(1 2 3)) => ((1) (2) (3))
At least one of the argument lists must be finite:
(zip '(3 1 4 1) (circular-list #f #t)) => ((3 #f) (1 #t) (4 #f) (1 #t))
unzip1
takes a list of lists, where every list must contain at
least one element, and returns a list containing the initial element of
each such list. That is, it returns (map car lists)
.
unzip2
takes a list of lists, where every list must contain at
least two elements, and returns two values: a list of the first
elements, and a list of the second elements.
unzip3
does the same for the first three elements of the lists,
and so forth.
(unzip2 '((1 one) (2 two) (3 three))) => (1 2 3) (one two three)
pred is a procedure taking as many arguments as there are lists and returning a single value. It is applied element–wise to the elements of the lists, and a count is tallied of the number of elements that produce a true value. This count is returned.
count
is “iterative” in that it is guaranteed to apply
pred to the list elements in a left–to–right order. The
counting stops when the shortest list expires.
(count even? '(3 1 4 1 5 9 2 5 6)) => 3 (count < '(1 2 4 8) '(2 4 6 8 10 12 14 16)) => 3
At least one of the argument lists must be finite:
(count < '(3 1 4 1) (circular-list 1 10)) => 2
Next: srfi list spec fold, Previous: srfi list spec select, Up: srfi list spec [Index]