Hi...
I have a problem using a member function
For example :
? (setf testword "One")
"One"
? (setf testlist ())
NIL
? (setf testlist (cons "One" testlist))
("One")
? (setf testlist (cons "Two" testlist))
("Two" "One")
? (member testword testlist)
NIL
Shouldn't it return T since "One" is in the testlist or I should another function for this situation?
Thank for help...
--
***********************************
{} Irma Sumera
/^\ ·······@cs.rmit.edu.au
/ 3rd year Computer Science
/
\..
***********************************
"Irma" <·······@cs.rmit.edu.au> wrote in message
···············@naylor.cs.rmit.edu.au...
> Hi...
> I have a problem using a member function
>
Hi, Irma...
This is because equality is not a simple thing in lisp (as in life!). Check
the hyperspec for the various flavors and what they mean. member takes a
keyword argument that specifies which equality test you want to use:
> (setf foo '("1" "2"))
("1" "2")
> (member "1" foo)
NIL
> (member "1" foo :test #'equalp)
("1" "2")
> (member "1" foo :test #'equal)
("1" "2")
> (member "1" foo :test #'eq)
NIL
> (member "1" foo :test #'string-equal)
("1" "2")
Coby
Irma <·······@cs.rmit.edu.au> wrote:
+---------------
| I have a problem using a member function ...
| ? (setf testlist (cons "Two" testlist))
| ("Two" "One")
| ? (member testword testlist)
| NIL
|
| Shouldn't it return T since "One" is in the testlist or I should
| another function for this situation?
+---------------
The problem is not "member" per se, but an interaction between
your choice of list element data type (string) and the function
"eql", which is the default two-argument test that "member" uses
if you don't specify a different one. That is:
> (eql 1 1)
T
> (member 1 '(2 1))
(1)
> (eql 'one 'one)
T
> (member 'one '(two one))
(ONE)
> (eql "one" "one")
NIL
> (member "one" '("two" "one"))
NIL
> (equal "one" "one") ; or any of equalp, string-equal, or string=
T
> (member "one" '("two" "one") :test #'equal)
("one")
>
Does that help?
-Rob
-----
Rob Warnock, 41L-955 ····@sgi.com
Network Engineering http://reality.sgi.com/rpw3/
Silicon Graphics, Inc. Phone: 650-933-1673
1600 Amphitheatre Pkwy. PP-ASEL-IA
Mountain View, CA 94043
In article <··········@naylor.cs.rmit.edu.au>, ·······@cs.rmit.edu.au
(Irma) wrote:
> Hi...
> I have a problem using a member function
>
> For example :
>
> ? (setf testword "One")
> "One"
> ? (setf testlist ())
> NIL
> ? (setf testlist (cons "One" testlist))
> ("One")
> ? (setf testlist (cons "Two" testlist))
> ("Two" "One")
> ? (member testword testlist)
> NIL
>
> Shouldn't it return T since "One" is in the testlist or I should another function for this situation?
MEMBER uses by default a test that can't check
whether two strings have the same content.
And we really have two strings, because the
testword and the first item of testlist are
not the *same* objects. Still they look the same.
But you're in luck, because the designers
of Common Lisp thought about that and
you can pass your own test via the :TEST
keyword. STRING= would be such a test.
? (member testword testlist :test #'string=)
("One")
So you are using a Mac. :-)
MEMBER item list &key :test :test-not :key
[Function]
searches list for a top-level element that matches item. If a match is
successful, member returns the rest of the list starting with the element
that matched item; otherwise returns nil.
STRING= string1 string2 &key :start1 :end1 :start2 :end2
[Function]
returns true if the specified portions of string1 and string2 are equal,
treating case as significant. The keywords :start and :end allow
comparison of substrings.
--
Rainer Joswig, Hamburg, Germany
Email: ·············@corporate-world.lisp.de
Web: http://corporate-world.lisp.de/