## The Mapping Functions

##### The differences between map, mapc, maplist, mapcar, mapcon and mapcan
The names of these 6 functions are historic, they have been in most Lisp variants since early on.

There is a kind of systematic pattern at work here. All 6 functions take an argument funtion and one or several lists. But they differ in whether they:
1. pass the whole (rest) list to the function, or only the CAR.
2. build a result list or not (i.e. whether they return something useful or are just for the side-effects).
3. build the result list destructively (using the equivalent of 'con' or 'conc') or non-destructively (using 'cons' or 'append').
Check it out.

#### A Chart!

```
Whole list    Only CAR
----------+-------------+-----------
No result |     map     |    mapc
|             |
append    |    maplist  |   mapcar
|             |
concat    |    mapcon   |   mapcan

```
Memorize this. Write it on a post-it note and stick it to the side of your monitor, tattoo it on your forearm, whatever you have to do.

The logic of the naming might be:
• The "c" in 'mapc' means "CAR", as does the "car" in 'mapcar'
• 'maplist' passes the whole list, while 'mapcar' takes the CAR
• The "con" in 'mapcon' means "concat" while the "can" in 'mapcan' replaces the "o" with an "a" from "CAR".
Note that you can implement ALL 6 functions using 'mapcon'. So 'mapcon' is the mother of all mapping functions (as also of 'find', 'filter' and so on). For example, instead of
```
: (mapcar inc (1 2 3))
-> (2 3 4)

```
you could write
```
: (mapcon '((L) (cons (inc (car L)))) (1 2 3))
-> (2 3 4)

```
```
: (mapc println (1 2 3))
1
2
3

```
you could write
```
: (mapcon '((L) (println (car L)) NIL) (1 2 3))
1
2
3

```
and so on.

In general, all functions in the "Only CAR" column above can be replaced by the "Whole list" functions if the argument function takes care of applying 'car' to the argument list.

I hope this clears things up a little.

https://picolisp.com/wiki/?mapping

 19mar16 erik
Revision History