Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What am I missing about make-symbol and assq here?

Tags:

elisp

I am trying to produce the same structure a let block accepts for local variable definitions but am hitting a wall: given this parse function:

(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)))

I get what looks like the sought output in lisp-interaction-mode:

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

Given that…

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

…I would expect the same result below – what am I missing?

(assq 'foo alist)
nil

While I'd be surprised if Emacs versions mattered, I've been testing this in Emacs 24.2 (9.0) on OSX.

like image 418
ecmanaut Avatar asked Apr 28 '13 05:04

ecmanaut


Video Answer


1 Answers

From the make-symbol documentation:

(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.

Your assq is comparing the interned symbol foo with some random uninterned symbol which happens to be named foo, which (of course) will fail, as they are not the same symbol.

Using intern instead of make-symbol (as below) may solve your problem.

(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") returns the interned symbol foo, which will be added to your alist, allowing (assq 'foo alist) to work just fine.

(Tested on my Emacs 24.2.1 on Win7.)

like image 135
michaelb958--GoFundMonica Avatar answered Jan 03 '23 18:01

michaelb958--GoFundMonica