anquegi
anquegi

Reputation: 11522

Nested mapcars in common lisp

I want to combine all posible combinations of two lists and for that I use mapcar

CL-USER> (mapcar #'(lambda (x) (mapcar #'(lambda (y) (list x y)) '(aa bb cc dd))) '(a b c))

(((A AA) (A BB) (A CC) (A DD)) ((B AA) (B BB) (B CC) (B DD))
 ((C AA) (C BB) (C CC) (C DD)))

the answer is correct but I get a nested list, how can I solve this. I would not like to flatten that list one level, I'm doing a bad use of mapcar but I cannot figure how to solve this

Upvotes: 3

Views: 555

Answers (1)

sds
sds

Reputation: 60004

Replace the top-level mapcar with mapcan and you are golden:

(mapcan (lambda (x) 
          (mapcar (lambda (y) (list x y))
                  '(aa bb cc dd)))
        '(a b c))
==> ((A AA) (A BB) (A CC) (A DD) 
     (B AA) (B BB) (B CC) (B DD)
     (C AA) (C BB) (C CC) (C DD))

Upvotes: 5

Related Questions