Next: , Previous: , Up: srfi ilists procs   [Index]


2.40.6.5 Miscellaneous: length, append, concatenate, reverse, zip and count

Function: integer ilength ilist

Return the length of its argument. It is an error to pass a value to ilength which is not a proper ilist (‘()’–terminated).

The length of a proper ilist is a non–negative integer n such that icdr applied n times to the ilist produces the empty list.

Function: ilist iappend ilist1

Return an ilist consisting of the elements of ilist1 followed by the elements of the other ilist parameters.

(iappend (iq x) (iq y))        ⇒  (x y)
(iappend (iq a) (iq b c d))    ⇒  (a b c d)
(iappend (iq a (b)) (iq (c)))  ⇒  (a (b) (c))

The resulting ilist is always newly allocated, except that it shares structure with the final ilisti argument. This last argument may be any value at all; an improper ilist results if it is not a proper ilist. All other arguments must be proper ilists.

(iappend (iq a b) (ipair 'c 'd))        ⇒ (a b c . d)
(iappend '() 'a)                        ⇒ a
(iappend (iq x y))                      ⇒ (x y)
(iappend)                               ⇒ ()
Function: value iconcatenate ilist-of-ilists

Append the elements of its argument together. That is, return:

(iapply iappend ilist-of-ilists)

or, equivalently:

(ireduce-right iappend '() ilist-of-ilists)

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 (iapply iappend ...) idiom would fail when applied to long lists, but iconcatenate would continue to function properly.

As with iappend, the last element of the input list may be any value at all.

Function: ilist ireverse ilist

Return a newly allocated ilist consisting of the elements of ilist in reverse order.

(ireverse (iq a b c))                   ⇒ (c b a)
(ireverse (iq a (b c) d (e (f))))       ⇒ ((e (f)) d (b c) a)
Function: ilist iappend-reverse rev-head tail

Return:

(iappend (ireverse 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 ilist, 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 the reverse and iappend-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.)

Function: ilist izip ilist1 ilist2

Equivalent to.

(lambda ilists (iapply imap ilist ilists))

If izip is passed n ilists, it returns an ilist as long as the shortest of these ilists, each element of which is an n–element ilist comprised of the corresponding elements from the parameter ilists.

(izip (iq one two three)
      (iq 1 2 3)
      (iq odd even odd even odd even odd even))
⇒ ((one 1 odd) (two 2 even) (three 3 odd))

(izip (iq 1 2 3))
⇒ ((1) (2) (3))
Function: ilist iunzip1 ilist
Function: ilist ilist iunzip2 ilist
Function: ilist ilist ilist iunzip3 ilist
Function: ilist ilist ilist ilist iunzip4 ilist
Function: ilist ilist ilist ilist ilist iunzip5 ilist

iunzip1 takes an ilist of ilists, where every ilist must contain at least one element, and returns an ilist containing the initial element of each such ilist. That is, it returns:

(imap icar ilists)

iunzip2 takes an ilist of ilists, where every ilist must contain at least two elements, and returns two values: an ilist of the first elements, and an ilist of the second elements.

iunzip3 does the same for the first three elements of the ilists, and so forth.

(iunzip2 (iq (1 one) (2 two) (3 three)))
⇒ (1 2 3) (one two three)
Function: integer icount pred ilist1 ilist2

pred is a procedure taking as many arguments as there are ilists and returning a single value. It is applied element–wise to the elements of the ilists, 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 ilist elements in a left–to–right order. The counting stops when the shortest ilist expires.

(count even? (iq 3 1 4 1 5 9 2 5 6))            ⇒ 3
(count < (iq 1 2 4 8) (iq 2 4 6 8 10 12 14 16)) ⇒ 3

Next: , Previous: , Up: srfi ilists procs   [Index]