informaticsEn 2021-12-13 18:06:30
168

Mapping

Mapping is a type of iteration in which a function is successively applied to pieces of one or more sequences. The result of the iteration is a sequence containing the respective results of the function applications.


Reference:

https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node90.html


Some examples:


list to string

CL-USER>  (map 'string (lambda (x) (char (write-to-string x) 0)) '(1 2 3))
"123"


string to list

CL-USER> (map 'list (lambda (x) (intern (string-upcase (string x)))) "lisp")
(L I S P)


list to vector

CL-USER> (map 'vector (lambda (x) x) '( 1 2 3))
#(1 2 3)


vector to string

CL-USER> (map 'list (lambda (x) x) #(1 2 3))
(1 2 3)


list to a-list

CL-USER> (map 'list (lambda (x)  (cons (car x) (cadr x))) '((1 "one") (2 "two")))
((1 . "one") (2 . "two"))