Identity of strings

Pascal Bourguignon pjb at informatimago.com
Mon Jan 23 17:55:50 UTC 2023


Le 23/01/2023 à 17:51, Andrea Monaco a écrit :
> 
> ANSI CL allows implementations to copy characters at any time.  Does
> that also applies to strings?  I think it should, because strings are
> made of characters...
> 
> In particular, I ask if my implementation can evaluate
> 
>    (eq (symbol-name 'aaa) (symbol-name 'aaa)) => nil
> 
> because it generates the name string at each call to SYMBOL-NAME.

The example is different from the question.

To answer the question, about the fact that characters (and numbers) may 
be copied at any time by the implementation, so that EQ may distinguish 
them,

    (let ((s "abc")) (eq (aref s 0) (aref s 0)))

may return true or NIL.

And notably:

   (let* ((c #\a) (s (string c))) (eq c (aref s 0)))

may return true or NIL too.


Concerning the example you give, it comes from another specification 
element, which is that SYMBOL-NAME may return a fresh string (or not). 
It is independent from the first question.

Note that the usual implementations all return T, ie. the same string, 
from SYMBOL-NAME.  But it is conceivable that an implementation does not 
store the symbol name, (for example, it could store the symbol in a 
tree-like data structure to spare the storage needed by the symbol names 
(which is for the CL package alone about 1/6 of a 64K byte memory). 
Then each time you call symbol-name a new string could be built from the 
tree data structure (the path to the symbol, from the root). In that 
case, your expression would indeed return NIL.

(let ((c 0))
   (do-external-symbols (s "CL")
   (incf c (length (symbol-name s))))
   c) ; --> 11271


In general, don't use EQ, instead use EQL, and don't use EQL on strings, 
but STRING=, STRING-EQUAL, or EQUAL or EQUALP.

-- 
__Pascal Bourguignon__




More information about the pro mailing list