From: Berk Birand
Subject: Modifying symbol names (symbol string conversion)
Date: 
Message-ID: <pan.2006.09.08.03.02.32.447143@yahoo.com>
Hi,

For one of my projects, I need to make changes on a symbol name. I
basically need to get rid of a ? character at the end of a symbol-name. If
I have
'green?
I want to be able to get
'green

I looked everywhere to find a solution to this but couldn't. The only
logical way of doing it seems to be to somehow convert 'green? to a
string, get rid of the question mark (by using some kind of string
manipulation function like substr), and then convert it back to a symbol.

Can this be done the way I described it? Is there a better way of doing it?

Thanks, 
Berk Birand

-- 
Posted via a free Usenet account from http://www.teranews.com

From: K.S.Sreeram
Subject: Re: Modifying symbol names (symbol string conversion)
Date: 
Message-ID: <1157690073.808371.210770@m79g2000cwm.googlegroups.com>
Berk Birand wrote:
> For one of my projects, I need to make changes on a symbol name. I
> basically need to get rid of a ? character at the end of a symbol-name. If
> I have
> 'green?
> I want to be able to get
> 'green
>
> I looked everywhere to find a solution to this but couldn't. The only
> logical way of doing it seems to be to somehow convert 'green? to a
> string, get rid of the question mark (by using some kind of string
> manipulation function like substr), and then convert it back to a symbol.
>
> Can this be done the way I described it? Is there a better way of doing it?

use (symbol-name 'some-symbol)

cheers
[sreeram;]
From: Berk Birand
Subject: Re: Modifying symbol names (symbol string conversion)
Date: 
Message-ID: <pan.2006.09.08.04.45.20.919182@yahoo.com>
On Thu, 07 Sep 2006 21:34:33 -0700, K.S.Sreeram wrote:

> 
> use (symbol-name 'some-symbol)
> 

First off, thanks for the reply!

So symbol-name returns the name of the symbol as a string. I then
can use string-trim to modify that string. But how do I transform the
result back to a symbol that I can compare with other symbols?

Cheers,
Berk


-- 
Posted via a free Usenet account from http://www.teranews.com
From: Frank Buss
Subject: Re: Modifying symbol names (symbol string conversion)
Date: 
Message-ID: <poyr85yq0e3h$.rcp1unljc6vj.dlg@40tude.net>
Berk Birand wrote:

> So symbol-name returns the name of the symbol as a string. I then
> can use string-trim to modify that string. But how do I transform the
> result back to a symbol that I can compare with other symbols?

You can use intern, but it sounds a bit strange to convert a symbol to a
string, strip some chars and convert it back to a symbol.

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Berk Birand
Subject: Re: Modifying symbol names (symbol string conversion)
Date: 
Message-ID: <pan.2006.09.08.05.01.20.819398@yahoo.com>
On Fri, 08 Sep 2006 06:52:26 +0200, Frank Buss wrote:


> You can use intern, but it sounds a bit strange to convert a symbol to a
> string, strip some chars and convert it back to a symbol.

Yeah I agree, but that's the only way I can think of to solve this
problem. If I have a symbol 'green?, I basically need to compare it
against the symbol 'green. I would appreciate any other ideas, but I will
look into intern meanwhile...

Thanks for the reply...

-- 
Posted via a free Usenet account from http://www.teranews.com
From: Bill Atkins
Subject: Re: Modifying symbol names (symbol string conversion)
Date: 
Message-ID: <m24pvjylj2.fsf@joshu.local>
Berk Birand <········@yahoo.com> writes:

> Yeah I agree, but that's the only way I can think of to solve this
> problem. If I have a symbol 'green?, I basically need to compare it
> against the symbol 'green. I would appreciate any other ideas, but I will
> look into intern meanwhile...

Can you give more details about where this symbol is coming from?  Why
do you need to perform this operation?
From: Berk Birand
Subject: Re: Modifying symbol names (symbol string conversion)
Date: 
Message-ID: <pan.2006.09.08.05.48.57.878876@yahoo.com>
On Fri, 08 Sep 2006 01:28:33 -0400, Bill Atkins wrote:

> 
> Can you give more details about where this symbol is coming from?  Why
> do you need to perform this operation?

The reason I'm using symbols this way is simply because at first it was
more convenient to do so. I'm reading a data file which is roughly
structure like Lisp (i.e. with parens ((action? arg arg)... ) ). I was
reading it with the read function, which directly parses everything as
symbols. And I never bothered to convert it to strings after that.

Since I have already written most of this application (which is my AI
project), I can't go back and change all the uses of symbols to strings.
Plus, it's due in 9 hours, so it's going to be a looong night anyway.

Meanwhile, I believe I have found a little "hack" that would let me
achieve what I need to do. Instead of converting the modified string value
to a symbol and then compare it, I will convert the other symbol to a
string, and compare the two as strings. If they match, then I'll keep them
in symbol format. I believe this should do the job for me.

Thank you all for your help. I will pay more attention to symbols and
strings next time I write LISP.

Regards,
Berk

-- 
Posted via a free Usenet account from http://www.teranews.com
From: Alberto Riva
Subject: Re: Modifying symbol names (symbol string conversion)
Date: 
Message-ID: <edqu1c$bs0s$1@usenet.osg.ufl.edu>
Berk Birand wrote:
> On Fri, 08 Sep 2006 06:52:26 +0200, Frank Buss wrote:
> 
> 
>> You can use intern, but it sounds a bit strange to convert a symbol to a
>> string, strip some chars and convert it back to a symbol.
> 
> Yeah I agree, but that's the only way I can think of to solve this
> problem. If I have a symbol 'green?, I basically need to compare it
> against the symbol 'green. I would appreciate any other ideas, but I will
> look into intern meanwhile...

Well, if you need to do this kind of operations, maybe you should use 
strings and not symbols in the first place. Is there any specific reason 
why you need (or you think you need) to work with symbols?

Alberto
From: Barry Margolin
Subject: Re: Modifying symbol names (symbol string conversion)
Date: 
Message-ID: <barmar-B47C00.21211208092006@comcast.dca.giganews.com>
In article <·············@usenet.osg.ufl.edu>,
 Alberto Riva <···@nospam.chip.org> wrote:

> Berk Birand wrote:
> > On Fri, 08 Sep 2006 06:52:26 +0200, Frank Buss wrote:
> > 
> > 
> >> You can use intern, but it sounds a bit strange to convert a symbol to a
> >> string, strip some chars and convert it back to a symbol.
> > 
> > Yeah I agree, but that's the only way I can think of to solve this
> > problem. If I have a symbol 'green?, I basically need to compare it
> > against the symbol 'green. I would appreciate any other ideas, but I will
> > look into intern meanwhile...
> 
> Well, if you need to do this kind of operations, maybe you should use 
> strings and not symbols in the first place. Is there any specific reason 
> why you need (or you think you need) to work with symbols?

Most of the string functions will accept symbols as well, automatically 
using the symbol's name as necessary.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Timofei Shatrov
Subject: Re: Modifying symbol names (symbol string conversion)
Date: 
Message-ID: <450119d4.4437590@news.readfreenews.net>
On Fri, 08 Sep 2006 01:01:20 -0400, Berk Birand <········@yahoo.com>
tried to confuse everyone with this message:

>On Fri, 08 Sep 2006 06:52:26 +0200, Frank Buss wrote:
>
>
>> You can use intern, but it sounds a bit strange to convert a symbol to a
>> string, strip some chars and convert it back to a symbol.
>
>Yeah I agree, but that's the only way I can think of to solve this
>problem. If I have a symbol 'green?, I basically need to compare it
>against the symbol 'green. I would appreciate any other ideas, but I will
>look into intern meanwhile...
>

Maybe you should compare symbol-names instead of symbols themselves. You
can abstract it into a separate function like compare-symbols.

Actially I had something like that in LIFP:

(defun flag-compare (flag1 flag2)
  "Tests whether flag2 unsets flag1"
  (let ((fl1 (symbol-name flag1))
        (fl2 (symbol-name flag2)))
    (and (char= (aref fl2 0) #\~) (string= fl1 fl2 :start2 1))))

(defun combine-flags (flaglist)
  "Combine a list of flags into a _set_ of flags"
  (loop for fl in flaglist
        if (char= (aref (symbol-name fl) 0) #\~) 
           do (setq set (nset-difference set (list fl) :test
#'flag-compare))
        else collect fl into set
        finally (return set)))

It allows flags to reset previous flags, for example flag :~light resets
previous flags :light, and so on.

-- 
|Don't believe this - you're not worthless              ,gr---------.ru
|It's us against millions and we can't take them all... |  ue     il   |
|But we can take them on!                               |     @ma      |
|                       (A Wilhelm Scream - The Rip)    |______________|
From: Rob Warnock
Subject: Re: Modifying symbol names (symbol string conversion)
Date: 
Message-ID: <IKudnXZWxuN4qJzYnZ2dnUVZ_rWdnZ2d@speakeasy.net>
Timofei Shatrov <····@mail.ru> wrote:
+---------------
| Maybe you should compare symbol-names instead of symbols themselves.
| You can abstract it into a separate function like compare-symbols.
| Actially I had something like that in LIFP:
| (defun flag-compare (flag1 flag2)
|   "Tests whether flag2 unsets flag1"
|   (let ((fl1 (symbol-name flag1))
|         (fl2 (symbol-name flag2)))
|     (and (char= (aref fl2 0) #\~) (string= fl1 fl2 :start2 1))))
+---------------

I sometimes find MISMATCH helpful in this kind of code,
plus you can easily have longer suffixes:

    > (defun compare/suffix (short long &optional (suffix "?"))
	"Compares SHORT with LONG and returns generalized truth if
	the symbol-name of SHORT concatenated with the string SUFFIX
	is the name as the symbol-name of LONG."
	(let* ((short-string (symbol-name short))
	       (long-string (symbol-name long))
	       (pos (mismatch short-string long-string)))
	  (if (numberp pos)
	    (not (mismatch suffix long-string :start2 pos))
	    (zerop (length suffix)))))

    COMPARE/SUFFIX
    > (mapcar 'compare/suffix '#1=(foo . #1#) '(foo foo? foo?? fooo))

    (NIL T NIL NIL)
    > (compare/suffix 'foo 'foobar "BAR")

    T
    > (compare/suffix '|| 'baz "BAZ") ; edge case: (zerop (length short))

    T
    > (compare/suffix 'foo 'foo "")   ; edge case: (zerop (length suffix))

    T
    > 

[The second edge case exposed a bug in my first try!!]

Of course, in this simple situation STRING= is fine by itself, too,
if you take the LENGTH of SHORT first:

    > (defun compare/suffix (short long &optional (suffix "?"))
	"Compares SHORT with LONG and returns generalized truth if
	the symbol-name of SHORT concatenated with the string SUFFIX
	is the name as the symbol-name of LONG."
	(let* ((short-string (symbol-name short))
	       (long-string (symbol-name long))
	       (short-len (length short-string)))
	  (and (string= short-string long-string :end2 short-len)
	       (string= suffix long-string :start2 short-len))))

    COMPARE/SUFFIX
    > (mapcar 'compare/suffix '#1=(foo . #1#) '(foo foo? foo?? fooo))

    (NIL T NIL NIL)
    > (list (compare/suffix 'foo 'foobar "BAR")
	    (compare/suffix '|| 'baz "BAZ")
	    (compare/suffix 'foo 'foo ""))

    (T T T)
    > 


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Thomas A. Russ
Subject: Re: Modifying symbol names (symbol string conversion)
Date: 
Message-ID: <ymi4pvi9vr4.fsf@sevak.isi.edu>
Berk Birand <········@yahoo.com> writes:

> On Fri, 08 Sep 2006 06:52:26 +0200, Frank Buss wrote:
> 
> 
> > You can use intern, but it sounds a bit strange to convert a symbol to a
> > string, strip some chars and convert it back to a symbol.
> 
> Yeah I agree, but that's the only way I can think of to solve this
> problem. If I have a symbol 'green?, I basically need to compare it
> against the symbol 'green. I would appreciate any other ideas, but I will
> look into intern meanwhile...

Well, perhaps if you describe the actual problem in more detail we can
provide alternative solutions.

In the meantime, why can't you just use either GREEN? or GREEN in both
places?  They are, after all, just symbols, so it doesn't really matter
which one you use in your code, right?

Where do the names GREEN and GREEN? come from in the first place?

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Ari Johnson
Subject: Re: Modifying symbol names (symbol string conversion)
Date: 
Message-ID: <m2ac5byqpn.fsf@hermes.theari.com>
Berk Birand <········@yahoo.com> writes:

> Hi,
>
> For one of my projects, I need to make changes on a symbol name. I
> basically need to get rid of a ? character at the end of a symbol-name. If
> I have
> 'green?
> I want to be able to get
> 'green
>
> I looked everywhere to find a solution to this but couldn't. The only
> logical way of doing it seems to be to somehow convert 'green? to a
> string, get rid of the question mark (by using some kind of string
> manipulation function like substr), and then convert it back to a symbol.
>
> Can this be done the way I described it? Is there a better way of doing it?

Do you just need a symbol with a modified name or do you need to
modify the name of a symbol in-place?  There is more to a symbol than
its name, and which one of these you need to do makes all the
difference.
From: Berk Birand
Subject: Re: Modifying symbol names (symbol string conversion)
Date: 
Message-ID: <pan.2006.09.08.03.51.23.605258@yahoo.com>
On Thu, 07 Sep 2006 23:36:36 -0400, Ari Johnson wrote:

> Do you just need a symbol with a modified name or do you need to
> modify the name of a symbol in-place?  There is more to a symbol than
> its name, and which one of these you need to do makes all the
> difference.

I believe I just need a symbol with a modified name. I will be comparing
it against some other symbols using (eq .. ..), so I don't need the
in-place modification.

Any ideas?
Thanks,
Berk

-- 
Posted via a free Usenet account from http://www.teranews.com