5

我正在尝试生成一个let块接受的用于局部变量定义的相同结构,但我碰壁了:给定这个parse函数:

(defun parse (string)
  (mapcar (lambda (line)
            (let* ((k_v (split-string line "="))
                   (key (make-symbol (first k_v)))
                   (val (second k_v)))
              (list key val)))
          (split-string string "\n" t)))

我在 lisp-interaction-mode 中得到了看起来像寻求的输出:

(setq alist (parse "foo=bar\nbaz=quux\n"))
((foo "bar") (baz "quux"))

鉴于……</p>

(assq 'foo '((foo "bar") (baz "quux")))
(foo "bar")

…我希望下面的结果相同——我错过了什么?

(assq 'foo alist)
nil

虽然如果 Emacs 版本很重要,我会感到惊讶,但我一直在 OSX 上的 Emacs 24.2 (9.0) 中对此进行测试。

4

1 回答 1

6

make-symbol文档中:

(make-symbol NAME)

Return a newly allocated uninterned symbol whose name is NAME.
Its value and function definition are void, and its property list is nil.

assq正在将实习符号与一些恰好名为的foo随机非实习foo符号进行比较,这(当然)会失败,因为它们不是同一个符号。

使用intern代替make-symbol(如下)可以解决您的问题。

(intern STRING &optional OBARRAY)

Return the canonical symbol whose name is STRING.
If there is none, one is created by this function and returned.
A second optional argument specifies the obarray to use;
it defaults to the value of `obarray'.
(defun parse (string)
  (mapcar (lambda (line)
            (let* ((k_v (split-string line "="))
                   (key (intern (first k_v))) ; change here
               (val (second k_v)))
              (list key val)))
          (split-string string "\n" t)))

(intern "foo")返回interned symbol foo,它将添加到您的 alist 中,允许(assq 'foo alist)正常工作。

(在 Win7 上的 Emacs 24.2.1 上测试过。)

于 2013-04-28T05:56:19.063 回答