Next: , Previous: Searching Lists, Up: Lists


7.7 Mapping of Lists

— procedure: map procedure list list ...

Procedure must be a procedure taking as many arguments as there are lists. If more than one list is given, then they must all be the same length. map applies procedure element-wise to the elements of the lists and returns a list of the results, in order from left to right. The dynamic order in which procedure is applied to the elements of the lists is unspecified; use for-each to sequence side effects.

          (map cadr '((a b) (d e) (g h)))           => (b e h)
          (map (lambda (n) (expt n n)) '(1 2 3 4))  => (1 4 27 256)
          (map + '(1 2 3) '(4 5 6))                 => (5 7 9)
          (let ((count 0))
            (map (lambda (ignored)
                   (set! count (+ count 1))
                   count)
                 '(a b c)))                         => unspecified
     
— procedure: map* initial-value procedure list1 list2 ...

Similar to map, except that the resulting list is terminated by initial-value rather than the empty list. The following are equivalent:

          (map procedure list list ...)
          (map* '() procedure list list ...)
     
— procedure: append-map procedure list list ...
— procedure: append-map* initial-value procedure list list ...

Similar to map and map*, respectively, 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 list list ...)
          (apply append (map procedure list list ...))
     
— procedure: append-map! procedure list list ...
— procedure: append-map*! initial-value procedure list list ...

Similar to map and map*, respectively, 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 list list ...)
          (apply append! (map procedure list list ...))
     
— procedure: for-each procedure list list ...

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 to the last, and the value returned by for-each is unspecified.

          (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)