thanks for all the responses in the string->number thread. I have another question: what is the correct result of
(case ".ab" ((".ab") 1) (else 2))
Indeed gsi gives another answer as scheme48. (maybe eq? vs equal?)
Christoph
Afficher les réponses par date
On Oct 31, 2008, at 9:48 AM, Christoph Bauer wrote:
thanks for all the responses in the string->number thread. I have another question: what is the correct result of
(case ".ab" ((".ab") 1) (else 2))
Indeed gsi gives another answer as scheme48. (maybe eq? vs equal?)
case use eqv?; two string with the same contents may not be eqv?.
Brad
Per Eckerdal wrote:
Ok, then my code is now rewritten as
(case (string->symbol ".ab") ((.ab) 1) (else 2))
Keep in mind, though, that interned symbols are not garbage collected, so string->symbol with arbitary input might result in memory leaks.
For which there is a workaround here: http://scheme.ch/gambit/preview/cj-symbol.scm (and http://scheme.ch/gambit/preview/cj-symbol-module.scm)
for those who didn't get this through irc support.
Anyway, a hash lookup will only outperform string=? if there are more than just a couple of strings to compare. I.e.
(cond ((string=? foo ".ab") ..) (else ..)) will be faster.
And, once you're using a hash lookup anyway, there's no point using a case statement which still does the linear search anyway. You could just map the string to a thunk then.
((table-ref map the-key default-thunk))
Christian.
On 31-Oct-08, at 9:48 AM, Christoph Bauer wrote:
thanks for all the responses in the string->number thread. I have another question: what is the correct result of
(case ".ab" ((".ab") 1) (else 2))
Indeed gsi gives another answer as scheme48. (maybe eq? vs equal?)
Case uses eqv? to compare the keys, and given that here (eqv? ".ab" ".ab") is normally #f, the else clause is executed. Note that (eqv? ".ab" ".ab") could be #t if compiled by a compiler that merges constants (Scheme48 seems to do this and Gambit used to do this, and may in the future). In any case in Scheme (eqv? ".ab" ".ab") is either #t or #f.
Marc