Reputation: 1444
If I compare two symbols using equal?
I get different results depending on whether one of the symbols is from a list and one is not.
This is demonstrated below:
; The following returns #t
(equal? (list-ref '('a 'b) 1) (list-ref '('a 'b) 1))
; But this return #f
(equal? 'b (list-ref '('a 'b) 1))
What is the best way to compare two symbols when one is from a list and one is not? If you can help me understand why this is the case then even better.
Upvotes: 1
Views: 2045
Reputation: 18917
You weren't comparing symbols:
> (list-ref '('a 'b) 1)
''b
> (symbol? (list-ref '('a 'b) 1))
#f
due to double quoting:
> (list-ref '(a b) 1)
'b
> (symbol? (list-ref '(a b) 1))
#t
So your initial case becomes
> (equal? 'b (list-ref '(a b) 1))
#t
If your list only contains symbols, it's more common to use eq? to compare them (it's supposed to be faster):
> (eq? 'b (list-ref '(a b) 1))
#t
If you double quoted because your list may contain other types, then use
> (list 'a 'b)
'(a b)
instead of
> '(a b)
'(a b)
Upvotes: 6