[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.2 srfi-1 - List library

Module: srfi-1

SRFI-1 is a rich collection of list manipulation library (SRFI-1). It is available by saying (use srfi-1). The implementation is based on Olin Shivers's reference implementation.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.2.1 List constructors

Function: xcons cd ca

[SRFI-1] Equivalent to (cons ca cd). Useful to pass to higher-order procedures.

Function: cons* elt1 elt2 …

[SRFI-1] Like list, but the last argument provides the tail of the constructed list. This is just a synonym of Gauche built-in procedure list*.

 
(cons* 1 2 3 4) ⇒ (1 2 3 . 4)
(cons* 1) ⇒ 1
Function: list-tabulate n init-proc

[SRFI-1] Constructs an n-element list, in which each element is generated by (init-proc i).

 
(list-tabulate 4 values) ⇒ (0 1 2 3)
Function: circular-list elt1 elt2 …

[SRFI-1] Constructs a circular list of the elements.

 
(circular-list 'z 'q) ⇒ (z q z q z q …)
Function: iota count &optional (start 0) (step 1)

[SRFI-1] Returns a list of numbers, starting from start, increasing by step.

 
(iota 5) ⇒ (0 1 2 3 4)
(iota 5 0 -0.1) ⇒ (0 -0.1 -0.2 -0.3 -0.4)

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.2.2 List predicates

Function: proper-list? x

[SRFI-1] Returns #t if x is a proper list.

Function: circular-list? x

[SRFI-1] Returns #t if x is a circular list.

Function: dotted-list? x

[SRFI-1] Returns #t if x is a finite, non-nil-terminated list. This includes non-pair, non-() values (e.g. symbols, numbers), which are considered to be dotted lists of length 0.

Function: null-list? list

[SRFI-1] Returns #t if list is the empty list (), and #f otherwise.

Function: not-pair? x

[SRFI-1] (lambda (x) (not (pair? x))).

SRFI-1 says: Provided as a procedure as it can be useful as the termination condition for list-processing procedures that wish to handle all finite lists, both proper and dotted.

Function: list= elt= list …

[SRFI-1] Determines list equality by comparing every n-th element of given lists by the procedure elt=.

It is an error to apply list= to anything except proper lists.

The equality procedure must be consistent with eq?, i.e.

 
(eq? x y) ⇒ (elt= x y).

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.2.3 List selectors

Function: first pair
Function: second pair
Function: third pair
Function: fourth pair
Function: fifth pair
Function: sixth pair
Function: seventh pair
Function: eighth pair
Function: ninth pair
Function: tenth pair

[SRFI-1] Returns n-th element of the (maybe improper) list.

Function: car+cdr pair

[SRFI-1] Returns two values, (car pair) and (cdr pair).

Function: take x i
Function: drop x i

[SRFI-1] 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:

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

drop is exactly equivalent to performing i cdr operations on x. The returned value shares a common tail with x. On the other hand, take always allocates a new list for result if the argument is a list of non-zero length.

An error is signalled if i is past the end of list x. See section util.list - Additional list library, for more tolerant version of take and drop.

For generic subsequence extraction from any sequence, see subseq in Slicing sequence.

Function: take-right flist i
Function: drop-right flist i

[SRFI-1] take-right returns the last i elements of flist. drop-right returns all but the last i elements of flist.

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

flist may be any finite list.

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

take-right's return value always shares a common tail with flist. drop-right always allocates a new list if the argument is a list of non-zero length.

An error is signalled if i is larger than the length of flist. See section util.list - Additional list library, for more tolerant version of take-right and drop-right.

Function: take! x i
Function: drop-right! x i

[SRFI-1] Linear update variants of take and drop-right. Those procedures may destructively modifies x.

If x is circular, take! may return a list shorter than expected.

Function: split-at x i
Function: split-at! x i

[SRFI-1] split-at splits the list x at index i, returning a list of the first i elements, and the remaining tail.

 
(split-at '(a b c d e) 2) ⇒ (a b) (c d e)

split-at! is the linear-update variant. It may destructively modifies x to produce the result.

Function: last pair

[SRFI-1] Returns the last element of the non-empty, finite list pair. It is equivalent to (car (last-pair pair)). Note that last-pair is Gauche built-in procedure.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.2.4 List miscellaneous routines

Function: length+ x

 EN [SRFI-1] If x is a proper list, returns its length. Otherwise, returns #f.

Function: concatenate list-of-lists
Function: concatenate! list-of-lists!

[SRFI-1] Equivalent to (apply append list-of-lists) and (apply append! list-of-lists), respectively.

Function: append-reverse rev-head tail
Function: append-reverse! rev-head tail

[SRFI-1] append-reverse returns (append (reverse rev-head) tail). append-reverse! is the linear-update variant.

Function: zip clist1 clist2 …

[SRFI-1] Equivalent to (map list clist1 clist2 …). 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))
Function: unzip1 list
Function: unzip2 list
Function: unzip3 list
Function: unzip4 list
Function: unzip5 list

[SRFI-1] 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. 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 on.

 
(unzip2 '((1 one) (2 two) (3 three))) ⇒
   (1 2 3) and
   (one two three)
Function: count pred clist1 clist2 …

[SRFI-1] A procedure pred is applied to the n-th element of given lists, from n is zero to the length of the the shortest finite list in the given lists, and the count of times pred returned true is returned.

 
(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

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.2.5 List fold, unfold & map

Function: fold kons knil clist1 clist2 …

[SRFI-1] The fundamental list iterator. When it is given a single list clist1 = (e1 e2en), then this procedure returns

 
(kons en … (kons e2 (kons e1 knil)) … ) 

If n list arguments are provided, then the kons function must take n+1 parameters: one element from each list, and the "seed" or fold state, which is initially knil. The fold operation terminates when the shortest list runs out of values. At least one of the list arguments must be finite.

Examples:

 
(fold + 0 '(3 1 4 1 5 9)) ⇒ 23 ;sum up the elements
(fold cons '() '(a b c d e)) ⇒ (e d c b a) ;reverse
(fold cons* '() '(a b c) '(1 2 3 4 5))
    ⇒ (c 3 b 2 a 1) ;n-ary case
Function: fold-right kons knil clist1 clist2 …

[SRFI-1] The fundamental list recursion operator. When it is given a single list clist1 = (e1 e2en), then this procedure returns

 
(kons e1 (kons e2 … (kons en knil)))

If n list arguments are provided, then the kons function must take n+1 parameters: one element from each list, and the "seed" or fold state, which is initially knil. The fold operation terminates when the shortest list runs out of values. At least one of the list arguments must be finite.

Examples:

 
(fold-right cons '() '(a b c d e))
   ⇒ (a b c d e) ;copy list
(fold-right cons* '() '(a b c) '(1 2 3 4 5))
   ⇒ (a 1 b 2 c 3) ;n-ary case
Function: pair-fold kons knil clist1 clist2 …
Function: pair-fold-right kons knil clist1 clist2 …

[SRFI-1] Like fold and fold-right, but the procedure kons gets each cdr of the given clists, instead of car.

Function: reduce f ridentity list
Function: reduce-right f ridentity list

[SRFI-1] Variant of fold and fold-right. f must be a binary operator, and ridentity is the value such that for any value x that is valid as f's input,

 
 (f x ridentity) ≡ x

These functions effectively do the same thing as fold or fold-right, respectively, but omit the first application of f to ridentity, using the above nature. So ridentity is used only when list is empty.

Function: unfold p f g seed &optional tail-gen

[SRFI-1] Fundamental recursive list constructor. Defined by the following recursion.

 
(unfold p f g seed tail-gen) ≡
   (if (p seed)
       (tail-gen seed)
       (cons (f seed)
             (unfold p f g (g seed))))

That is, p determines where to stop, g is used to generate successive seed value from the current seed value, and f is used to map each seed value to a list element.

Function: unfold-right p f g seed &optional tail

[SRFI-1] Fundamental iterative list constructor. Defined by the following recursion.

 
(unfold-right p f g seed tail) ≡
  (let lp ((seed seed) (lis tail))
    (if (p seed)
        lis
        (lp (g seed) (cons (f seed) lis))))
Function: append-map f clist1 clist2 …
Function: append-map! f clist1 clist2 …

[SRFI-1] Equivalent to

 
  (apply append (map f clist1 clist2 …))
  (apply append! (map f clist1 clist2 …))

At least one of the list arguments must be finite.

Function: map! f clist1 clist2 …

[SRFI-1] The procedure f is applied to each element of clist1 and corresponding elements of clist2s, and the result is collected to a list. Cells in clist1 is reused to construct the result list.

Function: map-in-order f clist1 clist2 …

[SRFI-1] A variant of map, but it guarantees to apply f on each elements of arguments in a left-to-right order. Since Gauche's map implementation follows the same order, this function is just a synonym of map.

Function: pair-for-each f clist1 clist2 …

[SRFI-1] Like for-each, but the procedure f is applied on each cdr of clists.

Function: filter-map f clist1 clist2 …

[SRFI-1] Like map, but only true values are saved. At least one of the list arguments must be finite.

 
(filter-map (lambda (x) (and (number? x) (* x x)))
            '(a 1 b 3 c 7))
  ⇒ (1 9 49)

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.2.6 List filtering & partitioning

Function: filter pred list
Function: filter! pred list

[SRFI-1] A procedure pred is applied on each element of list, and a list of elements that pred returned true on it is returned.

 
(filter odd? '(3 1 4 5 9 2 6)) ⇒ (3 1 5 9)

filter! is the linear-update variant. It may destructively modifies list to produce the result.

Function: remove pred list
Function: remove! pred list

[SRFI-1] A procedure pred is applied on each element of list, and a list of elements that pred returned false on it is returned.

 
(remove odd? '(3 1 4 5 9 2 6)) ⇒ (4 2 6)

remove! is the linear-update variant. It may destructively modifies list to produce the result.

Function: partition pred list
Function: partition! pred list

[SRFI-1] filter and remove simultaneously, i.e. returns two lists, the first is the result of filtering elements of list by pred, and the second is the result of removing elements of list by pred.

 
(partition odd? '(3 1 4 5 9 2 6))
  ⇒ (3 1 5 9) (4 2 6)

partition! is the linear-update variant. It may destructively modifies list to produce the result.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.2.7 List searching

Function: find pred clist

[SRFI-1] Applies pred for each element of clist, from left to right, and returns the first element that pred returns true on. If no element satisfies pred, #f is returned.

Function: find-tail pred clist

[SRFI-1] Applies pred for each element of clist, from left to right, and when pred returns a true value, returns the pair whose car is the element. If no element satisfies pred, #f is returned.

Function: take-while pred clist
Function: take-while! pred list

[SRFI-1] Returns the longest initial prefix of clist whose elements all satisfy pred.

Function: drop-while pred clist

[SRFI-1] Drops the longest initial prefix of clist whose elements all satisfy pred, and returns the rest.

Function: span pred clist
Function: span! pred list
Function: break pred clist
Function: break! pred list

[SRFI-1] span is equivalent to (values (take-while pred clist) (drop-while pred clist)). break inverts the sense of pred.

Function: any pred clist1 clist2 …

[SRFI-1] Applies pred across each element of clists, and returns as soon as pred returns a non-false value. The return value of any is the non-false value pred returned. If clists are exhausted before pred returns a non-false value, #f is returned.

Function: every pred clist1 clist2 …

[SRFI-1] Applies pred across each element of clists, and returns #f as soon as pred returns #f. If all application of pred return a non-false value, every returns the last result of the applications.

Function: list-index pred clist1 clist2 …

[SRFI-1] Returns the index of the leftmost element that satisfies pred. If no element satisfies pred, #f is returned.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.2.8 List deletion

Function: delete x list &optional elt=
Function: delete! x list &optional elt=

[SRFI-1] Equivalent to

 
  (remove (lambda (y) (elt= x y)) list)
  (remove! (lambda (y) (elt= x y)) list)

The comparison procedure, elt=, defaults to equal?.

Function: delete-duplicates list &optional elt=
Function: delete-duplicates! list &optional elt=

[SRFI-1] Removes duplicate elements from list. If there are multiple equal elements in list, the result list only contains the first or leftmost of these elements in the result. The order of these surviving elements is the same as in the original list. The comparison procedure, elt=, defaults to equal?.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.2.9 Association lists

Function: alist-cons key datum alist

[SRFI-1] Returns (cons (cons key datum) alist). This is an alias of the Gauche builtin procedure acons.

Function: alist-copy alist

[SRFI-1] Returns a fresh copy of alist. The spine of alist and each cell that points a key and a value is copied.

 
(define a (list (cons 'a 'b) (cons 'c 'd)))
a ⇒ ((a . b) (c . d))

(define b (alist-copy a))
b ⇒ ((a . b) (c . d))

(set-cdr! (car a) 'z)
a ⇒ ((a . z) (c . d))
b ⇒ ((a . b) (c . d))
Function: alist-delete key alist &optional =
Function: alist-delete! key alist &optional =

[SRFI-1] Deletes all cells in alist whose key is the same as key. Comparison is done by a procedure =. The default is eqv?.

The linear-update version alist-delete! may or may not modify alist.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.2.10 Lists as sets

These procedures use a list as a set, that is, the elements in a list matter, but their order doesn't.

All procedures in this category takes a comparison procedure elt=, as the first argument, which is used to determine two elements in the given sets are the same.

See also util.combinations - Combination library, which concerns combinations of elements in the set.

Function: lset<= elt= list1 …

[SRFI-1] Returns #t iff all elements in list1 are also included in list2, and so on. If no lists are given, or a single list is given, #t is returned.

Function: lset= elt= list1 list2 …

[SRFI-1] Returns #t if all elements in list1 are in list2, and all elements in list2 are in list1, and so on.

 
(lset= eq? '(b e a) '(a e b) '(e e b a)) ⇒ #t
Function: lset-adjoin elt= list elt …

[SRFI-1] Adds elt … to the set list, if each one is not already a member of list. (The order doesn't matter).

 
(lset-adjoin eq? '(a b c) 'a 'e) ⇒ '(e a b c)
Function: lset-union elt= list1 …

[SRFI-1] Returns the union of the sets list1 ….

Function: lset-intersection elt= list1 list2 …

[SRFI-1] Returns a set of elements that are in every lists.

Function: lset-difference elt= list1 list2 …

[SRFI-1] Returns a set of elements that are in list1 but not in list2. In n-ary case, binary differece operation is simply folded.

Function: lset-xor elt= list1 …

[SRFI-1] Returns the exclusive-or of given sets; that is, the returned set consists of the elements that are in either list1 or list2, but not in both. In n-ary case, binary xor operation is simply folded.

Function: lset-diff+intersection elt= list1 list2 …

[SRFI-1] Returns two sets, a difference and an intersection of given sets.

Function: lset-union! elt= list …
Function: lset-intersection! elt= list1 list2 …
Function: lset-difference! elt= list1 list2 …
Function: lset-xor! elt= list1 …
Function: lset-diff+intersection! elt= list1 list2 …

[SRFI-1] Linear update variant of the corresponding procedures. The cells in the first list argument may be reused to construct the result.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated by Shiro Kawai on October, 7 2008 using texi2html 1.78.