"for comprehension" in Common Lisp

Suppose you have a list and want to create new list by applying some algorithms.

Common Lisp provides collect funcion for this. For example, let's say we have a list of string in lower-case letters and we want to capitalize all the strings in list. We can do that as follows:

(defvar names '("james" "maria" "rico" "peter"))

CL-USER> (loop for name in names
             collect (string-capitalize name))
("James" "Maria" "Rico" "Peter")

Or let's say you want new list which contains length of each string in list, then you can do following:

CL-USER> (loop for name in names
             collect (length name))
(5 5 4 5)

Or let's say you want a new list with all entries in upper-case, then you can do the following:

CL-USER> (loop for name in names
             collect (string-upcase name))
("JAMES" "MARIA" "RICO" "PETER")

But, lisp is pretty cool functional language. We can also use map function to achieve same results. Pay attention to map function signature below:

(map result-type function &rest sequences+)

Here, you have to specify expected result-type. In our case it will be 'list.

For example: to capitalize all entries in list of string.

CL-USER> (map 'list #'string-capitalize names)
("James" "Maria" "Rico" "Peter")

or, to get new list with length of each entry:

CL-USER> (map 'list #'length names)
(5 5 4 5)

or, to convert all elements of list to uppercase:

CL-USER> (map 'list #'string-upcase names)
("JAMES" "MARIA" "RICO" "PETER")