The less-familiar parts of Lisp for beginners — map

Our next less-well-known Lisp function is map.  This function works like the more-familiar mapcar, but rather than necessarily returning a list, returns a sequence of the type chosen in the invocation.

Adapting the example in the CLHS:
 

CL-USER> (map 'string
              #'(lambda (x y)
                  (char "01234567890ABCDEF"
                        (mod (+ x y) 16)))
       '(1 2 3 4)
       '(7 8 9 10))
"80BD"
CL-USER> (map 'vector
              #'(lambda (x y)
                  (char "01234567890ABCDEF"
                        (mod (+ x y) 16)))
       '(1 2 3 4)
       '(7 8 9 10))
#(#\8 #\0 #\B #\D)
CL-USER> (map 'list
              #'(lambda (x y)
                  (char "01234567890ABCDEF"
                        (mod (+ x y) 16)))
       '(1 2 3 4)
       '(7 8 9 10))
(#\8 #\0 #\B #\D)
CL-USER> (mapcar
              #'(lambda (x y)
                  (char "01234567890ABCDEF"
                        (mod (+ x y) 16)))
       '(1 2 3 4)
       '(7 8 9 10))
(#\8 #\0 #\B #\D)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

反垃圾邮件 / Anti-spam question * Time limit is exhausted. Please reload CAPTCHA.