GNU Info

Info Node: (emacs-lisp-intro.info)mapcar

(emacs-lisp-intro.info)mapcar


Next: Another Bug Prev: lambda Up: Print Whole Graph
Enter node , (file) or (file)node

The `mapcar' Function
---------------------

   `mapcar' is a function that calls its first argument with each
element of its second argument, in turn.  The second argument must be a
sequence.

   The `map' part of the name comes from the mathematical phrase,
`mapping over a domain', meaning to apply a function to each of the
elements in a domain.  The mathematical phrase is based on the metaphor
of a surveyor walking, one step at a time, over an area he is mapping.
And `car', of course, comes from the Lisp notion of the first of a list.

For example,

     (mapcar '1+ '(2 4 6))
          => (3 5 7)

The function `1+' which adds one to its argument, is executed on _each_
element of the list, and a new list is returned.

   Contrast this with `apply', which applies its first argument to all
the remaining.  (Note: Readying a Graph, for a
explanation of `apply'.)

   In the definition of `one-fiftieth', the first argument is the
anonymous function:

     (lambda (arg) (/ arg 50))

and the second argument is `full-range', which will be bound to
`list-for-graph'.

   The whole expression looks like this:

     (mapcar '(lambda (arg) (/ arg 50)) full-range))

   Note: Mapping Functions, for more about
`mapcar'.

   Using the `one-fiftieth' function, we can generate a list in which
each element is one-fiftieth the size of the corresponding element in
`list-for-graph'.

     (setq fiftieth-list-for-graph
           (one-fiftieth list-for-graph))

   The resulting list looks like this:

     (10 20 19 15 11 9 6 5 4 3 3 2 2
     1 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 4)

This, we are almost ready to print!  (We also notice the loss of
information: many of the higher ranges are 0, meaning that fewer than
50 defuns had that many words or symbols--but not necessarily meaning
that none had that many words or symbols.)


automatically generated by info2www version 1.2.2.9