MAPCAR, APPLY, FUNCTION #', LAMBDA, MAPPEND
Examples
2]> (defun square (x) (* x x))
SQUARE
[3]> (square 3)
9
[4]> (mapcar #'square '(10 20 30 40 50 60 70))
(100 400 900 1600 2500 3600 4900)
[5]>(setf *words* '((one un)
(two deux)
(three trois)
(four quatre)
(five cinq)))
((ONE UN) (TWO DEUX) (THREE TROIS) (FOUR QUATRE) (FIVE CINQ))
[6]> (mapcar #'car *words*)
(ONE TWO THREE FOUR FIVE)
[7]> (mapcar #'cadr *words*)
(UN DEUX TROIS QUATRE CINQ)
[8]> (mapcar #'reverse *words*)
((UN ONE) (DEUX TWO) (TROIS THREE) (QUATRE FOUR) (CINQ FIVE))
[9]> (defun translate (x) (cadr (assoc x *words*)))
TRANSLATE
[10]> (assoc 'three *words*)
(THREE TROIS)
[11]> (mapcar #'translate '(three one four one five))
(TROIS UN QUATRE UN CINQ)
[12]> (mapcar #'+ '(1 2 3 4 5) '(10 20 30 40 50))
(11 22 33 44 55)
[16]> (setf names '((John Q. Public) (Malcolm X)
(Admiral Grace Murray Hopper) (Spot)
(Aristotle) (A A Milne) (Z Z Top)
(Sir Larry Oliver) (Miss Scarlet)))
((JOHN Q. PUBLIC) (MALCOLM X) (ADMIRAL GRACE MURRAY HOPPER) (SPOT) (ARISTOTLE)
(A A MILNE) (Z Z TOP) (SIR LARRY OLIVER) (MISS SCARLET)
)
[17]> (defun last-name (name)
(first (last name)))
LAST-NAME
[18]> (mapcar #'last-name names)
(PUBLIC X HOPPER SPOT ARISTOTLE MILNE TOP OLIVER SCARLET)
[19]> (defparameter *titles* '(Mr Mrs Miss Ms Sir Madam Dr Admiral Major General))
*TITLES*
[20]> (defun first-name (name)
(if (member (first name) *titles*)
(first-name (rest name))
(first name)))
FIRST-NAME
[21]> (mapcar #'first-name names)
(JOHN MALCOLM GRACE SPOT ARISTOTLE A Z LARRY SCARLET)
[22]> (first-name '(Madam Major General Paula Jones))
PAULA
[23]> (trace first-name)
;; Tracing function FIRST-NAME.
(FIRST-NAME)
[24]> (first-name '(Madam Major General Paula Jones))
1. Trace: (FIRST-NAME '(MADAM MAJOR GENERAL PAULA JONES))
2. Trace: (FIRST-NAME '(MAJOR GENERAL PAULA JONES))
3. Trace: (FIRST-NAME '(GENERAL PAULA JONES))
4. Trace: (FIRST-NAME '(PAULA JONES))
4. Trace: FIRST-NAME ==> PAULA
3. Trace: FIRST-NAME ==> PAULA
2. Trace: FIRST-NAME ==> PAULA
1. Trace: FIRST-NAME ==> PAULA
PAULA
[25]> (untrace first-name)
(FIRST-NAME)
[26]> (first-name '(Mr Blue Jeans))
BLUE
[27]> (defun mappend (fn the-list)
"Apply fn to each element of list and append the results."
;; the string above is known as a documentation string
(apply #'append (mapcar fn the-list)))
MAPPEND
[28]> (apply #'+ '(1 2 3 4 5 6 7 8 9 10))
55
[29]> (apply #'append '((1 2 3) (a b c)))
(1 2 3 A B C)
[30]> (defun self-and-double (x) (list x (+ x x)))
SELF-AND-DOUBLE
[31]> (self-and-double 3)
(3 6)
[32]> (apply #'self-and-double '(3))
(3 6)
[33]> (mapcar #'self-and-double '(1 10 300))
((1 2) (10 20) (300 600))
[34]> (mappend #'self-and-double '(1 10 300))
(1 2 10 20 300 600)
[35]> (lambda (x) (+ x 2))
#<CLOSURE :LAMBDA (X) (+ X 2)>
[36]> ((lambda (x) (+ x 2)) 4)
6
[37]> (funcall #'(lambda (x) (+ x 2)) 4)
6
[38]> (mapcar #'(lambda (x) (+ x x))
'(1 2 3 4 5))
(2 4 6 8 10)
[39]> (mappend #'(lambda (lst) (list lst (reverse lst)))
'((1 2 3) (a b c)))
((1 2 3) (3 2 1) (A B C) (C B A))