Tutorial by Examples

A set of high-level mapping functions is available in Common Lisp, to apply a function to the elements of one or more lists. They differ in the way in which the function is applied to the lists and how the final result is obtained. The following table summarize the differences and shows for each of ...
MAPCAR is the most used function of the family: CL-USER> (mapcar #'1+ '(1 2 3)) (2 3 4) CL-USER> (mapcar #'cons '(1 2 3) '(a b c)) ((1 . A) (2 . B) (3 . C)) CL-USER> (mapcar (lambda (x y z) (+ (* x y) z)) '(1 2 3) '(10 20 30) '(1...
CL-USER> (maplist (lambda (list) (cons 0 list)) '(1 2 3 4)) ((0 1 2 3 4) (0 2 3 4) (0 3 4) (0 4)) CL-USER> (maplist #'append '(a b c d -) '(1 2 3)) ((A B C D - 1 2 3) (B C D - 2 3) (C D - 3))
MAPCAN: CL-USER> (mapcan #'reverse '((1 2 3) (a b c) (100 200 300))) (3 2 1 C B A 300 200 100) CL-USER> (defun from-to (min max) (loop for i from min to max collect i)) FROM-TO CL-USER> (from-to 1 5) (1 2 3 4 5) CL-USER> (mapcan #'from-to '(1 2 3) '(5 5 5)) (1 2 3 4 5...
MAPC: CL-USER> (mapc (lambda (x) (print (* x x))) '(1 2 3 4)) 1 4 9 16 (1 2 3 4) CL-USER> (let ((sum 0)) (mapc (lambda (x y) (incf sum (* x y))) '(1 2 3) '(100 200 300)) sum) 1400 ; => (1 x 100) + (2 x 200) + (3 x 30...

Page 1 of 1