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


7.7 Mapping of Lists

standard procedure: map procedure list list …

It is an error if procedure does not accept as many arguments as there are lists and return a single value.

The map procedure applies procedure element-wise to the elements of the lists and returns a list of the results, in order. If more than one list is given and not all lists are the same length, map terminates when the shortest list runs out. The lists can be circular, but it is an error if all of them are circular. It is an error for procedure to mutate any of the lists. The dynamic order in which procedure is applied to the elements of the lists is unspecified. If multiple returns occur from map, the values returned by earlier returns are not mutated.

(map cadr '((a b) (d e) (g h)))         ⇒ (b e h)
(map (lambda (n) (expt n n)) '(1 2 3 4 5))
                                        ⇒ (1 4 27 256 3125)
(map + '(1 2 3) '(4 5 6 7))             ⇒ (5 7 9)
(let ((count 0))
  (map (lambda (ignored)
         (set! count (+ count 1))
         count)
       '(a b)))                         ⇒ (1 2) or (2 1)
obsolete procedure: map* knil proc list1 list2

Deprecated, use fold-right instead. Equivalent to

(fold-right (lambda (e1 e2 … acc)
              (cons* (proc e1)
                     (proc e2)
                     …
                     acc))
            knil
            list1
            list2
            …)
SRFI 1 procedure: append-map procedure list list …

Similar to map except that the results of applying procedure to the elements of lists are concatenated together by append rather than by cons. The following are equivalent, except that the former is more efficient:

(append-map procedure list1 list2 …)
(apply append (map procedure list1 list2 …))
obsolete procedure: append-map* knil proc list1 list2

Deprecated, use fold-right instead. Equivalent to

(fold-right (lambda (e1 e2 … acc)
              (append (proc e1)
                      (proc e2)
                      …
                      acc))
            knil
            list1
            list2
            …)
SRFI 1 procedure: append-map! proc list list …

Similar to map except that the results of applying proc to the elements of lists are concatenated together by append! rather than by cons. The following are equivalent, except that the former is more efficient:

(append-map! proc list list …)
(apply append! (map proc list list …))
obsolete procedure: append-map*! knil proc list1 list2

Deprecated, use fold-right instead. Equivalent to

(fold-right (lambda (e1 e2 … acc)
              (append! (proc e1)
                       (proc e2)
                       …
                       acc))
            knil
            list1
            list2
            …)
standard procedure: for-each procedure list list …

It is an error if procedure does not accept as many arguments as there are lists.

The arguments to for-each are like the arguments to map, but for-each calls procedure for its side effects rather than for its values. Unlike map, for-each is guaranteed to call procedure on the elements of the lists in order from the first element(s) to the last, and the value returned by for-each is unspecified. If more than one list is given and not all lists have the same length, for-each terminates when the shortest list runs out. The lists can be circular, but it is an error if all of them are circular.

It is an error for procedure to mutate any of the lists.

(let ((v (make-vector 5)))
  (for-each (lambda (i)
              (vector-set! v i (* i i)))
            '(0 1 2 3 4))
  v)                            ⇒ #(0 1 4 9 16)
SRFI 1 procedure: any predicate list list …

Applies predicate across the lists, returning true if predicate returns true on any application.

If there are n list arguments list1listn, then predicate must be a procedure taking n arguments and returning a boolean result.

any applies predicate to the first elements of the list parameters. If this application returns a true value, any immediately returns that value. Otherwise, it iterates, applying predicate to the second elements of the list parameters, then the third, and so forth. The iteration stops when a true value is produced or one of the lists runs out of values; in the latter case, any returns #f. The application of predicate to the last element of the lists is a tail call.

Note the difference between find and anyfind returns the element that satisfied the predicate; any returns the true value that the predicate produced.

Like every, any’s name does not end with a question mark—this is to indicate that it does not return a simple boolean (#t or #f), but a general value.

(any integer? '(a 3 b 2.7))   => #t
(any integer? '(a 3.1 b 2.7)) => #f
(any < '(3 1 4 1 5)
       '(2 7 1 8 2)) => #t
SRFI 1 procedure: every predicate list list …

Applies predicate across the lists, returning true if predicate returns true on every application.

If there are n list arguments list1listn, then predicate must be a procedure taking n arguments and returning a boolean result.

every applies predicate to the first elements of the list parameters. If this application returns false, every immediately returns false. Otherwise, it iterates, applying predicate to the second elements of the list parameters, then the third, and so forth. The iteration stops when a false value is produced or one of the lists runs out of values. In the latter case, every returns the true value produced by its final application of predicate. The application of predicate to the last element of the lists is a tail call.

If one of the lists has no elements, every simply returns #t.

Like any, every’s name does not end with a question mark—this is to indicate that it does not return a simple boolean (#t or #f), but a general value.


Next: Folding of Lists, Previous: Searching Lists, Up: Lists   [Contents][Index]