About me

About me

Feeds

RSS feed

What use is 'mapcon'?

13th June 2024

I was recently reading about the map functions in Edmund Weitz's excellent book "Common Lisp Recipes". He gives this good summary of them:

  Gives no output Gives a list of return values Concatenates return values
Processes items mapc mapcar mapcan
Processes tails mapl maplist mapcon

It occurred to me that I've never used mapcon, so I asked on the LispWorks forum if anyone could suggest why you might want to use it. Martin Simmons pointed me to this nice example in the LispWorks documentation.

It uses mapcon in conjunction with mapcar to create a list of all pairs of a list of elements:

(defun pairs (lst)
  (mapcon
   (lambda (x)
     (mapcar 
      (lambda (y) (list (car x) y))
      (cdr x)))
   lst))

> (pairs '(a b c d e))
((a b) (a c) (a d) (a e) (b c) (b d) (b e) (c d) (c e) (d e))

blog comments powered by Disqus