From: ······@gmail.com
Subject: Problem comparing symbols
Date: 
Message-ID: <1169549680.090311.271760@l53g2000cwa.googlegroups.com>
Hi all,

I've been getting some weird bugs, and I need some help figuring what's
going on.  I've distilled the problem down to something simple, but I
still don't know why it's not working.  Here's the code snippet:

(defpackage #:test-package
  (:use :cl)
  (:export
   #:test-function)
  (:nicknames #:tp))

(defpackage #:test2
  (:use :cl :tp)
  (:nicknames #:t2))

(in-package test-package)

(defun test-function (my-symbol)
  (if (equal my-symbol 'hello)
      (print "working")
    (print "not-working")))

And here's output in my listener:


CL-USER 1 > (in-package tp)
#<PACKAGE TEST-PACKAGE>

TP 2 > (test-function 'hello)

"working"
"working"

TP 3 > (in-package t2)
#<PACKAGE TEST2>

TEST2 4 > (test-function 'hello)

"not-working"
"not-working"

So, what is going on?  Why does the function in the test2 package not
have the same results as the one in test package?

Thanks in advance!

From: ······@gmail.com
Subject: Re: Problem comparing symbols
Date: 
Message-ID: <1169550858.915858.271710@k78g2000cwa.googlegroups.com>
······@gmail.com wrote:
> TEST2 4 > (test-function 'hello)
This call is equivalent to (test-function 'TEST2::hello)
Will this be enough information to figure out your problem?
From: Stefan Kamphausen
Subject: Re: Problem comparing symbols
Date: 
Message-ID: <85bqkqdmde.fsf@usenet.my.skamphausen.de>
Hi,

reading

http://www.flownet.com/gat/packages.pdf

might help here.


······@gmail.com writes:

> Hi all,
>
> I've been getting some weird bugs, and I need some help figuring what's
> going on.  I've distilled the problem down to something simple, but I
> still don't know why it's not working.  Here's the code snippet:
>
> (defpackage #:test-package
>   (:use :cl)
>   (:export
>    #:test-function)
>   (:nicknames #:tp))
>
> (defpackage #:test2
>   (:use :cl :tp)
>   (:nicknames #:t2))
>
> (in-package test-package)
>
> (defun test-function (my-symbol)
>   (if (equal my-symbol 'hello)
>       (print "working")
>     (print "not-working")))

try:

(defun test-function (my-symbol)
  (print (symbol-package my-symbol))
  (if (equal my-symbol 'hello)
    (print "working")
    (print "not-working")))


AFAICT the symbol 'HELLO is interned into the TEST2-PACKAGE when you
are calling (test-function 'hello) after changing into that package.
But you are comparing that to the 'HELLO symbol interned in the
TEST-PACKAGE when the function definition was read.


If I am mistaken, I'm sure some lispers with more experience will jump
in :-)


Regards,
Stefan
-- 
Stefan Kamphausen --- http://www.skamphausen.de
a blessed +42 regexp of confusion (weapon in hand)
You hit. The format string crumbles and turns to dust.
From: Pascal Bourguignon
Subject: Re: Problem comparing symbols
Date: 
Message-ID: <87hcuiotwl.fsf@thalassa.informatimago.com>
······@gmail.com writes:
> (defun test-function (my-symbol)
>   (if (equal my-symbol 'hello)
>       (print "working")
>     (print "not-working")))

My general advice will be to always refer to the reference, CLHS.
It says that EQUAL applied on symbols is equivalent to EQ.

Perhaps you want STRING-EQUAL or STRING=.
Or perhaps you need to cater to the hints given in the other answers.


-- 
__Pascal_Bourguignon__               _  Software patents are endangering
()  ASCII ribbon against html email (o_ the computer industry all around
/\  1962:DO20I=1.100                //\ the world http://lpf.ai.mit.edu/
    2001:my($f)=`fortune`;          V_/   http://petition.eurolinux.org/
From: Thomas A. Russ
Subject: Re: Problem comparing symbols
Date: 
Message-ID: <ymir6tlcnzy.fsf@sevak.isi.edu>
······@gmail.com writes:

And if all the other advice fails:

> Hi all,
> 
> I've been getting some weird bugs, and I need some help figuring what's
> going on.  I've distilled the problem down to something simple, but I
> still don't know why it's not working.  Here's the code snippet:


> (defun test-function (my-symbol)

 (let ((*package* (find-package "CL")))
   (print 'hello)
   (print my-symbol))

>   (if (equal my-symbol 'hello)
>       (print "working")
>     (print "not-working")))




-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Alan Crowe
Subject: Re: Problem comparing symbols
Date: 
Message-ID: <863b618y39.fsf@cawtech.freeserve.co.uk>
······@gmail.com writes:

> Hi all,
> 
> I've been getting some weird bugs, and I need some help figuring what's
> going on.  I've distilled the problem down to something simple, but I
> still don't know why it's not working.  Here's the code snippet:
> 
> (defpackage #:test-package
>   (:use :cl)
>   (:export
>    #:test-function)
>   (:nicknames #:tp))
> 
> (defpackage #:test2
>   (:use :cl :tp)
>   (:nicknames #:t2))
> 
> (in-package test-package)
> 
> (defun test-function (my-symbol)
>   (if (equal my-symbol 'hello)
>       (print "working")
>     (print "not-working")))
> 

Since you have used three symbols not in the CL package
there are now three symbols interned in test-package

TP> *package*
#<The TEST-PACKAGE package, 2/8 internal, 1/2 external>

two internal, one external. (the 8 and the second 2 are
telling you that for efficiency reasons space has alread
been allocated in the expectation of more symbols to come)

1)test-function which was there already due to the export

2)my-symbol a variable name

3)hello which is quoted and is being used as data

> And here's output in my listener:
> 
> 
> CL-USER 1 > (in-package tp)
> #<PACKAGE TEST-PACKAGE>
> 
> TP 2 > (test-function 'hello)
> 
> "working"
> "working"
> 
> TP 3 > (in-package t2)
> #<PACKAGE TEST2>
> 
> TEST2 4 > (test-function 'hello)
> 
> "not-working"
> "not-working"
> 
> So, what is going on?  Why does the function in the test2 package not
> have the same results as the one in test package?
> 

You didn't export tp::hello, so the when the reader reads

(test-function 'hello)

with *package* bound to t2 it makes a new symbol t2::hello
distinct from tp::hello.

If you had exported tp::hello from tp, when the reader
searched the packages used by t2 it would have found
tp:hello and used it instead of making a new symbol.

The usual idea for solving this problem is not to export
hello from tp, but to use a keyword :hello, that is all the
symbols that you use as tags go in the keyword package. 

You have probably already seen this done in other code. It
is the default behaviour for keyword arguments, and can be
over-ridden like this:

CL-USER> (defun foo (&key hello ((hello goodbye)))
           (list hello goodbye))

CL-USER> (foo :hello 'greetings)
(GREETINGS NIL)

CL-USER> (foo 'hello 'farewell)
(NIL FAREWELL)

CL-USER> (foo :hello '|take me to your leader|
              'hello 'exterminate)
(|take me to your leader| EXTERMINATE)

By playing games with whether you export hello or not you
can obfuscate your code in totally evil ways :-)

Alan Crowe
Edinburgh
Scotland