1

I'm trying to compare (quote foo) with 'foo using equal, but the equality is failing. Here is a trace of what is happening with my code. I don't understand why the comparison fails -- does it have something to do with the reader, or chained evaluation?

How can write the code to make the comparison succeed? Thanks

(plist-get (gethash "key" table) 'type)
(quote ctkb)

(equal 'ctkb (quote ctkb))
t

(equal 'ctkb (plist-get (gethash "key" table) 'type))
nil
4
  • 2
    It looks like you're comparing 'ctkb with '(quote ctkb) ?
    – phils
    May 28 '16 at 22:50
  • @phils: Please post that as an answer.
    – Drew
    May 28 '16 at 22:53
  • Are they not the same thing to the reader? I thought I could replace 'foo with (quote foo) whenever it suited me, because the reader treated them the same. As evidence, see the middle line in my original posting, where "equal" says they are the same. The puzzle to me is why equality succeeds in the middle line, using my "manual" version of what is returned in line 1, but fails in the 3rd line, where the return value of line 1 is passed directly into the equality test.
    – Kevin
    May 29 '16 at 1:26
  • If it helps any, I stored 'ctkb into the hash table originally, even though gethash is returning (quote ctkb) as a result in line 1.
    – Kevin
    May 29 '16 at 1:33
2

It looks like you're comparing 'ctkb with '(quote ctkb) -- or (quote (quote ctkb)) if you prefer.

e.g. Evaluating '(quote ctkb) returns the value (quote ctkb), exactly like your plist-get return value.

(equal 'ctkb (quote ctkb)) is certainly true, but (equal 'ctkb '(quote ctkb)) isn't the same thing.

I presume that (equal 'ctkb (cadr (plist-get (gethash "key" table) 'type))) holds (and indeed eq).

To say anything more, we'd need to see the code you're using to populate the values -- I don't see any unexpected quoting being introduced when I set up a similar structure:

(setq table (make-hash-table))
(setq pl nil)
(setq pl (plist-put pl 'type 'ctkb))
(puthash 'k pl table)
(eq (plist-get (gethash 'k table) 'type) 'ctkb)
t
3
  • Wow, you are correct. I played around with how I stored the original values, and can get the desired results. And you are also right about (cadr ...) working, which pulls ctkb out of the return result. Could you please tell me exactly what syntax you saw that made you conclude that "it looks like I'm comparing 'ctkb with (quote (quote ctkb))"? My eyes tell me the return value from line 1 is (quote ctkb), not (quote (quote ctkb)). Or phrased another way, can the evaluator ever return 'ctkb? (using a single quote, instead of the longer (quote xxx) which I usually see. Thanks again.
    – Kevin
    May 29 '16 at 6:53
  • I think I see now. In line 2, the reader treats the single quote in 'ctkb and the (quote ...) as the same thing, so the equality succeeds. But when I see a return value of (quote ctkb), that's the evaluator (pretty printer) telling me that the actual result is 'ctkb. So in line 3 (equal 'ctkb <result>) actually becomes (equal ctkb (quote ctkb)) for evaluation, which gives nil. I've just learned a nuance of reading vs evaluation syntax. Thanks again.
    – Kevin
    May 29 '16 at 7:06
  • Yes, when you evaluate 'FORM or (quote FORM) (as the reader converts the former to the latter) the output is just FORM. Quoting a form means that the evaluated value is simply what was quoted. The evaluated value is not then quoted (again) for printing; you simply see the printed representation of the object in question. As you've correct ascertained, in your case you were trying to compare the symbol ctkb to an object with a printed representation of (quote ctkb), whereas the symbol's printed representation is just ctkb.
    – phils
    May 29 '16 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.