listemacs

Emacs-Lisp lists, apostrophe ('), and backquote (grave accent, `)


I'm an emacs newbie and was going through this tutorial: https://learnxinyminutes.com/docs/elisp/

I wrote this code:

(defun hello (name)
  (insert (format "Hello %s!\n" name)))

(setq list-of-names '("John", "Jane", "Chad")) ;; -1-

(mapcar `hello list-of-names) ;;-2-

and when I did C-xC-e at line 2 the result came out as:

Hello John!
Hello (, Jane)!
Hello (, Chad)!

So I looked up what apostrophe does and it's a short hand for quote.

    (setq list-of-names (quote("John", "Jane", "Chad"))) ;; -1-

and results are the same. But when I accidentally replaced apostrophe with a grave accent(`, or SHIFT + ~ in my keyboard) it printed out correctly.

Hello John!
Hello Jane!
Hello Chad!

I've spent an hour looking up what grave accent does but no luck so far. Does anyone know what grave accent means in emacs?

I'm using emacs-w32 with Korean/English keyboard.


Solution

  • ` is similar to ', but then you can use , to prefix symbols that won't be quoted. See "Backquote" in the Elisp manual.

    Lists in lisp aren't comma separated.

    (setq list-of-names `("John" "Jane" "Chad" ,(current-time-string)))
    (mapcar 'hello list-of-names)