0

I am using \n so that when printing the list, each element in the list gets a new line when calling message. I am trying to have a go at printing a list where each element does not have a \n at the end. What commands would achieve printing each element in the list in a separate line?

(defvar hcom '("basic\n"))
(push "+ inix-frame\n" hcom)
(push "+ modust-launch\n" hcom)
(push "+ automode + speedbar\n" hcom)
(push "+ modelin + txscale\n" hcom)
(message "%s" (mapconcat #'identity (reverse hcom))) 

Had a go with the following, but this makes an extra empty line between list elements.

(setq animals '(giraffe gazelle lion tiger))

(defun print-elements-of-list (list)
  "Print each element of LIST on a line of its own."
  (interactive)
  (while list
    (print (car list))
    (setq list (cdr list))))

(print-elements-of-list animals)

1 Answer 1

1

You can use prin1 or princ for that (see Elisp documentation on 'output functions')

(dolist (animal '(giraffe gazelle lion tiger))
  (princ animal)
  (princ "\n"))

If you'd prefer to use mapconcat, then you can just pass the newline as separator:

(mapconcat #'symbol-name '(giraffe gazelle lion tiger) "\n")
2
  • In what ways is symbol-name better than identity. You are implying that symbol-name is better, right?
    – Dilna
    Commented Oct 13, 2022 at 22:18
  • mapconcat only works with strings while I have given it a list of symbols, for a list of strings identity would be the way to go... Commented Oct 13, 2022 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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