Howto recreate a symbol created by `make-symbol`

Viewed 67

My program creates dummy data. Part of these dummy data are keys in the form of symbols: (lambda (i) (make-symbol(format nil"~@r"i))) (fixnum values with their Roman numeral as symbol as key). The program stores these dummy key value pairs in a data structure i'm developing.

I tried to use these symbols from the REPL to test retrieving the values by their key. However:

(equal (make-symbol "IX") (make-symbol "IX"))

NIL

(equal (make-symbol "IX") 'IX))

NIL

Is there a way to enter a symbol previously created with make-symbol in the REPL so that both are equal?

2 Answers

As @coredump suggests, use intern to add these symbols into some package, like that:

CL-USER> (defpackage :my-symbols)
#<Package "MY-SYMBOLS">
CL-USER> (intern "IX" :my-symbols)
MY-SYMBOLS::IX
NIL
CL-USER> (intern "IX" :my-symbols)
MY-SYMBOLS::IX
:INTERNAL
CL-USER> (eql (intern "IX" :my-symbols)
              (intern "IX" :my-symbols))
T
CL-USER>

One can use string= to compare symbols by name:

CL-USER 29 > (string= (make-symbol "IX") (make-symbol "IX"))
T

CL-USER 30 > (string= (make-symbol "IX") (make-symbol "Ix"))
NIL

string-equal is the case insensitive variant.

Related