This is the mail archive of the
kawa@sources.redhat.com
mailing list for the Kawa project.
Re: (eq? sym1 sym2) implementation
- From: Jim White <jim at pagesmiths dot com>
- To: S D <s dot mailinglists at gmail dot com>
- Cc: kawa at sources dot redhat dot com
- Date: Sat, 19 Mar 2005 15:08:26 -0800
- Subject: Re: (eq? sym1 sym2) implementation
- References: <301e20f605031911516de2586d@mail.gmail.com>
The behavior you're seeing is correct. Kawa has both interned an
uninterned symbols as described in R5RS. If you get a symbol (or
java.lang.String in your code) from a source that does not intern then
you're gonna have to intern it in order to get eq? to work for them.
I think it poor style to depend on this aspect of Kawa's implementation
(that symbols are j.l.String) and beware that it may break in the
future. Also it would be rare/unexpected for the intern/eq? code to be
faster than using string=? (which does accept j.l.String) when the
number of cond clauses is not very large because intern has to do a fair
number of comparisons itself (it hashes to a bucket then does sequential
search). Where interning is useful is when the symbols are interned on
read and then identity comparisons are done many times for each symbol,
not just once.
You can get the same coding style without unnecessary dependency (and no
interning) with case-equal (attached):
(case-equal attribute-value
("val1" ...)
("val2" ...)
(else ...))
Jim
(define-syntax case-equal
(syntax-rules (else)
((case-equal (key ...) clauses ...)
(let ((keyval (key ...)))
(case-equal keyval clauses ...)))
((case-equal key (else result1 ...))
(begin result1 ...))
((case-equal key ((values ...) result1 ...))
(if (member key '(values ...))
(begin result1 ...)))
((case-equal key ((values ...) result1 ...) clauses ...)
(if (member key '(values ...))
(begin result1 ...)
(case-equal key clauses ...)))
((case-equal key (value1 result1 ...))
(if (equal? key value1)
(begin result1 ...)))
((case-equal key (value1 result1 ...) clauses ...)
(if (equal? key value1)
(begin result1 ...)
(case-equal key clauses ...)))
))