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:
- pass the whole (rest) list to the function, or only the CAR.
- build a result list or not (i.e. whether they return something useful or are just for the side-effects).
- build the result list destructively (using the equivalent of 'con' or 'conc') or non-destructively (using 'cons' or 'append').
A Chart!
Whole list Only CAR ----------+-------------+----------- No result | map | mapc | | append | maplist | mapcar | | concat | mapcon | mapcanMemorize 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".
: (mapcar inc (1 2 3)) -> (2 3 4)you could write
: (mapcon '((L) (cons (inc (car L)))) (1 2 3)) -> (2 3 4)or instead of
: (mapc println (1 2 3)) 1 2 3you could write
: (mapcon '((L) (println (car L)) NIL) (1 2 3)) 1 2 3and 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 |