Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
I have the following structure:
'((a (1 2 3 4)) (b (1 2)) (c (1 3 4 5))
I want to return a new list that's similar, but that has a 7 inserted after each 2:
7
2
'((a (1 2 7 3 4)) (b (1 2 7)) (c (1 3 4 5))
How can I do this?
mapcar
You need a nested mapcar and mapcan:
mapcan
(mapcar (lambda (l) (list (car l) (mapcan (lambda (x) (if (= x 2) (list x 7) (list x))) (cadr l)))) '((a (1 2 3 4)) (b (1 2)) (c (1 3 4 5)))) ==> ((a (1 2 7 3 4)) (b (1 2 7)) (c (1 3 4 5)))
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
mapcar
is non-destructive, i.e., it does not mutate its arguments.