Movatterモバイル変換


[0]ホーム

URL:


[LISPWORKS][Common Lisp HyperSpec (TM)][Previous][Up][Next]


FunctionMAPC, MAPCAR, MAPCAN, MAPL, MAPLIST, MAPCON

Syntax:

mapcfunction&rest lists+ =>list-1

mapcarfunction&rest lists+ =>result-list

mapcanfunction&rest lists+ =>concatenated-results

maplfunction&rest lists+ =>list-1

maplistfunction&rest lists+ =>result-list

mapconfunction&rest lists+ =>concatenated-results

Arguments and Values:

function---adesignator for afunction that must take as manyarguments as there arelists.

list---aproper list.

list-1---the firstlist (which must be aproper list).

result-list---alist.

concatenated-results---alist.

Description:

The mapping operation involves applyingfunction to successive sets of arguments in which one argument is obtained from eachsequence. Except formapc andmapl, the result contains the results returned byfunction. In the cases ofmapc andmapl, the resultingsequence islist.

function is called first on all the elements with index0, then on all those with index1, and so on.result-type specifies thetype of the resultingsequence. Iffunction is asymbol, it iscoerced to afunction as if bysymbol-function.

mapcar operates on successiveelements of thelists.function is applied to the firstelement of eachlist, then to the secondelement of eachlist, and so on. The iteration terminates when the shortestlist runs out, and excess elements in other lists are ignored. The value returned bymapcar is alist of the results of successive calls tofunction.

mapc is likemapcar except that the results of applyingfunction are not accumulated. Thelist argument is returned.

maplist is likemapcar except thatfunction is applied to successive sublists of thelists.function is first applied to thelists themselves, and then to thecdr of eachlist, and then to thecdr of thecdr of eachlist, and so on.

mapl is likemaplist except that the results of applyingfunction are not accumulated;list-1 is returned.

mapcan andmapcon are likemapcar andmaplist respectively, except that the results of applyingfunction are combined into alist by the use ofnconc rather thanlist. That is,

 (mapcon f x1 ... xn)   ==  (apply #'nconc (maplist f x1 ... xn))
and similarly for the relationship betweenmapcan andmapcar.

Examples:

 (mapcar #'car '((1 a) (2 b) (3 c))) =>  (1 2 3)  (mapcar #'abs '(3 -4 2 -5 -6)) =>  (3 4 2 5 6) (mapcar #'cons '(a b c) '(1 2 3)) =>  ((A . 1) (B . 2) (C . 3)) (maplist #'append '(1 2 3 4) '(1 2) '(1 2 3)) =>  ((1 2 3 4 1 2 1 2 3) (2 3 4 2 2 3))  (maplist #'(lambda (x) (cons 'foo x)) '(a b c d))=>  ((FOO A B C D) (FOO B C D) (FOO C D) (FOO D)) (maplist #'(lambda (x) (if (member (car x) (cdr x)) 0 1)) '(a b a c d b c))=>  (0 0 1 0 1 1 1);An entry is 1 if the corresponding element of the input;  list was the last instance of that element in the input list. (setq dummy nil) =>  NIL  (mapc #'(lambda (&rest x) (setq dummy (append dummy x)))        '(1 2 3 4)        '(a b c d e)        '(x y z)) =>  (1 2 3 4)  dummy =>  (1 A X 2 B Y 3 C Z)                    (setq dummy nil) =>  NIL  (mapl #'(lambda (x) (push x dummy)) '(1 2 3 4)) =>  (1 2 3 4)  dummy =>  ((4) (3 4) (2 3 4) (1 2 3 4))  (mapcan #'(lambda (x y) (if (null x) nil (list x y)))          '(nil nil nil d e)          '(1 2 3 4 5 6)) =>  (D 4 E 5)  (mapcan #'(lambda (x) (and (numberp x) (list x)))          '(a 1 b c 3 4 d 5))=>  (1 3 4 5)
In this case the function serves as a filter; this is a standard Lisp idiom usingmapcan.

 (mapcon #'list '(1 2 3 4)) =>  ((1 2 3 4) (2 3 4) (3 4) (4))

Affected By: None.

Exceptional Situations:

Should be prepared to signal an error oftypetype-error if anylist is not aproper list.

See Also:

dolist,map,Section 3.6 (Traversal Rules and Side Effects)

Notes: None.


The followingX3J13 cleanup issues,not part of the specification, apply to this section:


[Starting Points][Contents][Index][Symbols][Glossary][Issues]
Copyright 1996-2005, LispWorks Ltd. All rights reserved.


[8]ページ先頭

©2009-2025 Movatter.jp