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)
Dilna
  • 1,173
  • 3
  • 10

1 Answers1

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")
dalanicolai
  • 6,108
  • 7
  • 23
  • In what ways is `symbol-name` better than `identity`. You are implying that `symbol-name` is better, right? – Dilna Oct 13 '22 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... – dalanicolai Oct 13 '22 at 22:21