From: charlieb
Subject: Can (eq (make-symbol "NAME") 'NAME) ever be t
Date: 
Message-ID: <2nej5bFtqgU1@uni-berlin.de>
Hi,
	The question's in the Subject, but what I'm trying to do is make a 
symbol into a string, change it then make the new string back into a 
different symbol.

The task-dialog takes an alist with a numeric head, '(12 (STATUS . 
OPEN)) is a simple example.
For each item in the alist it will make a labelled text dialog.
When the OK button is pressed the two lower functions try to 
re-construct the task keeping the cdr types the same as before.

What I want is for the task-dialog to result in e.g.  '(12 (STATUS . 
CLOSED)) not  '(12 (STATUS . #:CLOSED)) nor  '(12 (STATUS . "CLOSED"))
The reason for this is that 'CLOSED is a special symbol that is used 
elsewhere when displaying the task in a different window.

I could switch to using strings but I thought I ought to explore my 
options first.

Cheers,
Charlieb.

Here's the code:
(use-package 'ltk)

(defun task-dialog (task &optional task-fn)
   (let* ((textfields nil)
	 (win (make-instance 'toplevel))
	 (ok-b (make-instance 'button
	      :master win
	      :text "OK"
	      :command (lambda ()
			 (update-task (re-create-task task textfields)))))
	 (cancel-b (make-instance 'button
				  :master win
				  :text "Cancel"
				  :command (lambda () (destroy win)))))
     (dolist (item (cdr task) t)
       (let* ((fr (make-instance 'frame :master win))
	     (text (make-instance 'entry :master fr))
	     (label (make-instance 'label :master fr :text (car item))))
	(setf (text text) (cdr item))
	(pack fr :expand t :fill :both)
	(pack text :side :right :expand t :fill :x)
	(pack label :side :left :padx 5)
	(push text textfields)))
     (pack ok-b :side :left :expand t :fill :x)
     (pack cancel-b :side :right :expand t :fill :x)))

(defun re-create-task (task textfields)
   (cons (car task)
	(loop for field in (cdr task)
	      for value in (reverse textfields)
	      collect (cons (car field)
			    (string-to-type-of (text value) (cdr field))))))

(defun string-to-type-of (string var)
   (print var)
   (typecase var
     (SYMBOL (make-symbol (string-upcase string)))
     (INTEGER (parse-integer string))
     (STRING string)))

From: Pascal Costanza
Subject: Re: Can (eq (make-symbol "NAME") 'NAME) ever be t
Date: 
Message-ID: <cet7su$8vd$1@newsreader2.netcologne.de>
charlieb wrote:

> Hi,
>     The question's in the Subject, but what I'm trying to do is make a 
> symbol into a string, change it then make the new string back into a 
> different symbol.

According to the ANSI spec, the symbol returned by make-symbol is a 
"fresh, uninterned symbol", whereas the symbol NAME must be interned in 
some package. If it wasn't by the time you entered the above form, it 
would be interned then because of that form.

This means that these two symbols cannot be EQ.


Pascal

-- 
Tyler: "How's that working out for you?"
Jack: "Great."
Tyler: "Keep it up, then."
From: charlieb
Subject: Re: Can (eq (make-symbol "NAME") 'NAME) ever be t
Date: 
Message-ID: <2nenopF2qenU1@uni-berlin.de>
Pascal Costanza wrote:

> According to the ANSI spec, the symbol returned by make-symbol is a 
> "fresh, uninterned symbol", whereas the symbol NAME must be interned in 
> some package. If it wasn't by the time you entered the above form, it 
> would be interned then because of that form.
> 
> This means that these two symbols cannot be EQ.

OK I suspected that this was the case, thanks for the confirmation.
Can I get around this in some way so that perhaps (eq (my-make-symbol 
"NAME") 'NAME) => t

I tried (setf (symbol-name 'test) "sucess") but (setf symbol-name) is 
undefined (at least in clisp and sbcl).

Cheers,
Charlie
From: Don Geddis
Subject: Re: Can (eq (make-symbol "NAME") 'NAME) ever be t
Date: 
Message-ID: <87ekmll76b.fsf@sidious.geddis.org>
charlieb <··@privacy.net> wrote on Thu, 05 Aug 2004:
> Can I get around this in some way so that perhaps (eq (my-make-symbol "NAME")
> 'NAME) => t

How about INTERN?

    -- Don
_______________________________________________________________________________
Don Geddis                  http://don.geddis.org/               ···@geddis.org
From: ·········@random-state.net
Subject: Re: Can (eq (make-symbol "NAME") 'NAME) ever be t
Date: 
Message-ID: <cetafi$5126r$2@midnight.cs.hut.fi>
charlieb <··@privacy.net> wrote:

> OK I suspected that this was the case, thanks for the confirmation.
> Can I get around this in some way so that perhaps (eq (my-make-symbol 
> "NAME") 'NAME) => t

Don't use MAKE-SYMBOL, but rather stick those symbols in a package with
INTERN:

 (defvar *syms* (make-package "SYMBOLS"))
 (eq (intern "FOO" *syms*) 'symbols::foo) => T

Or just use keywords:

 (eq (intern "FOO" :keyword) :foo) => T

Cheers,

 -- Nikodemus                   "Not as clumsy or random as a C++ or Java. 
                             An elegant weapon for a more civilized time."
From: Juho Snellman
Subject: Re: Can (eq (make-symbol "NAME") 'NAME) ever be t
Date: 
Message-ID: <slrnch4bgk.hfj.jsnell@melkinpaasi.cs.Helsinki.FI>
<··@privacy.net> wrote:
>OK I suspected that this was the case, thanks for the confirmation.
>Can I get around this in some way so that perhaps (eq (my-make-symbol 
>"NAME") 'NAME) => t

Sure, (eq (intern "NAME") 'name). However, this won't give you
_different_ symbols, which you requested in your original message.
Different symbols are by definition not EQ.

-- 
Juho Snellman
From: charlieb
Subject: Re: Can (eq (make-symbol "NAME") 'NAME) ever be t
Date: 
Message-ID: <2ner0hF4gqkU1@uni-berlin.de>
Thank-you all, I think intern is a great solution, I've tested it 
in-situ and it works exactly the way I wanted.

Cheers
Charlie.
From: Alan Crowe
Subject: Re: Can (eq (make-symbol "NAME") 'NAME) ever be t
Date: 
Message-ID: <86pt64vwqk.fsf@cawtech.freeserve.co.uk>
Charlie wrote
> Thank-you all, I think intern is a great solution, I've
> tested it in-situ and it works exactly the way I wanted.

I think the three main symbol-returning functions are

MAKE-SYMBOL - for brand new symbols that are used in machine
              written code. You store the symbol in a
              variable and use it from there. The symbol has
              a name, but the symbol is not registered with
              any package, so it cannot be refered to by
              name, which is very useful for avoiding name
              clashes between symbols the programmer enters
              at the keyboard and symbols in machine
              generated code.

INTERN - reuses an existing symbol, creates a new symbol if
         there is no existing symbol. The function used by READ.

FIND-SYMBOL - reuse an existing symbol, never create a new symbol.

It looks like FIND-SYMBOL is the tool for the job. If a
coding error results in a string that does not name an
existing symbol, you probably don't want INTERN to create a
symbol for you, because that only delays discovering the
error.

Indeed you could wrap find-symbol in a function written to
signal an error for use in those cases in which you are
looking for a symbol that must already exist.

(defun find-existing-symbol(string &optional (package *package*))
    (multiple-value-bind (symbol already-exists)
	(find-symbol string package)
      (unless already-exists
	(error "String ~S does not name a pre-existing symbol ·@
                in ~A."
	       string package))
      symbol))

For example:

(defpackage "BAR"
    (:export "FOO"))

(find-existing-symbol "FOO" "BAR") => BAR:FOO

(find-existing-symbol "FOO")
Error in function FIND-EXISTING-SYMBOL:
   String "FOO" does not name a pre-existing symbol 
in #<The COMMON-LISP-USER package, 5/9 internal, 0/9 external>.

Restarts:
  0: [ABORT] Return to Top-Level.

Debug  (type H for help)

Alan Crowe
Edinburgh
Scotland
From: Björn Lindberg
Subject: Re: Can (eq (make-symbol "NAME") 'NAME) ever be t
Date: 
Message-ID: <hcshdrhlplg.fsf@my.nada.kth.se>
charlieb <··@privacy.net> writes:

> Pascal Costanza wrote:
> 
> > According to the ANSI spec, the symbol returned by make-symbol is a
> > "fresh, uninterned symbol", whereas the symbol NAME must be interned
> > in some package. If it wasn't by the time you entered the above
> > form, it would be interned then because of that form.
> > This means that these two symbols cannot be EQ.
> 
> OK I suspected that this was the case, thanks for the confirmation.
> Can I get around this in some way so that perhaps (eq (my-make-symbol
> "NAME") 'NAME) => t

I'm not completely sure what you are trying to do in the overall
picture, but for this problem it seems to me that you could use the
reader mechanisms, or more specifically READ-FROM-STRING:

  (eq 'foo (read-from-string "foo"))
  ==> T

The reader will return the same symbol, if it already exists in the
current package.


Bj�rn