0

So simple question, i'm not sure which function would be best to do this. How do you add a string to each item in a list? So example:

(setq list '("Sample Data" "More Fun"))

And then say I want to add " is great." to each item in my list so it reads:

(print list)
"Sample Data is great."
"More Fun is great."
1

Simple mapping over lists is done with mapcar. Concatenating strings works in principle with concat. But, in most cases mapconcat is better.

(setq list '("Sample Data" "More Fun"))

(setq list (mapcar (lambda (str)
                       (concat str " is great"))
                   list))

Example for mapconcat: Let us assume that you have given a variable tail with string value "is great". The space before is is missing. mapconcat inserts the separator between the strings.

(let ((list '("Sample Data" "More Fun"))
      (tail "is great"))

  (setq list (mapcar (lambda (str)
                        (mapconcat #'identity (list str tail) " "))
                     list)))

I also use a let form here instead of a simple setq avoiding the polution of the global namespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.