Next: , Up: Mapping Functions   [Index]


F.2.3.1 Function mapcar

mapcar applies function to each element of sequence in turn, and returns a list of the results.

Function: mapcar function sequence

Apply ‘FUNCTION’ to each element of ‘SEQUENCE’, and make a list of the results.

The result is a list (always) just as long as ‘SEQUENCE’.

SEQUENCE’ may be a list, a vector, a bool-vector, or a string (not char-table).

Examples of mapcar

(mapcar 'car '((a b) (c d) (e f)))
     ⇒ (a c e)
(mapcar '1+ [1 2 3])
     ⇒ (2 3 4)
(mapcar 'string "abc")
     ⇒ ("a" "b" "c")

;; Call each function in my-hooks.
(mapcar 'funcall my-hooks)
  (defun mapcar* (function &rest args)
    "Apply FUNCTION to successive cars of all ARGS.
  Return the list of results."
    ;; If no list is exhausted,
    (if (not (memq nil args))
        ;; apply function to CARs.
        (cons (apply function (mapcar 'car args))
              (apply 'mapcar* function
                     ;; Recurse for rest of elements.
                     (mapcar 'cdr args)))))

;  (mapcar* 'cons '(a b c) '(1 2 3 4))
;       ⇒ ((a . 1) (b . 2) (c . 3))

Listing F.1: Define Function @{mapcar*}