Search code examples
emacscoding-styleelispletbackticks

plist creating with backticks fails in let expression


I have a twofold question and hope for advice from the experts here.

1) In a syntactic analysis of some code, I need to store the components found for later use. I now consider storing these as property list (an isolated one, not the one of the string containing the code, as this would seems to me like quite an abuse although it would be convenient). Is this against all conventions or a reasonable way to deal with this situation?

2) I fail to create a property list in a (let ... ) statement.

This works:

(setq x "BAR")
(setq pl `(bar ,x))
(setq pl (plist-put pl 'foo "FOO"))
(plist-get pl 'foo)  ; returns "FOO"

But this doesn't:

(let (pl `(bar ,x))
  (setq pl (plist-put pl 'foo "FOO"))
  (plist-get pl 'foo))

Emacs complains about the void function bar. It obviously tries to evaluate (bar ...) despite the backtick. Why?


Solution

  • For lack of parentheses. Let's expand the special backtick notation:

    (let (pl (\` (bar (\, x))))
      (setq pl (plist-put pl 'foo "FOO"))
      (plist-get pl 'foo))
    

    This declares a variable pl (with no initial value, and hence initialized to nil) as well as a variable named ` (that's right: its name is "backtick") initialized to the value of (bar (\, x)).

    So, you want to write the following instead:

    (let ((pl `(bar ,x)))
      ...)