From: Padu
Subject: Position of lists within lists
Date: 
Message-ID: <k8-dnYla4KJ_Y03d4p2dnA@iswest.net>
One more basic question.

If I have a given list (setq x '(a b c)) and I want to find the index of
element 'b in that list, I know that position works well
(position 'b x) => 1

now, imagine that my list is composed of tuples:
(setq x '((1 0) (1 1) (1 2) (2 0) (2 1) (2 2)))
and I want to find what is the position of element (1 2)
I would use pos again,
(position '(1 2) x) => NIL
but it returns nil... am I using the wrong function or there's simply no way
to do it using a single function (and therefore I'll have to create a small
function for that)?

Thanks

Padu

From: André Thieme
Subject: Re: Position of lists within lists
Date: 
Message-ID: <caqvqu$3gr$1@ulric.tng.de>
Padu schrieb:

> One more basic question.
> 
> If I have a given list (setq x '(a b c)) and I want to find the index of
> element 'b in that list, I know that position works well
> (position 'b x) => 1
> 
> now, imagine that my list is composed of tuples:
> (setq x '((1 0) (1 1) (1 2) (2 0) (2 1) (2 2)))
> and I want to find what is the position of element (1 2)
> I would use pos again,
> (position '(1 2) x) => NIL
> but it returns nil... am I using the wrong function or there's simply no way
> to do it using a single function (and therefore I'll have to create a small
> function for that)?
> 
> Thanks

POSITION works nearly like you think it would.
The test on equality is done by comparing the address of the object 
inside the ram.
Whenever you type something quoted it gets a new address.

See here:

CL-USER 9 > (setf obj '(1 2 3))
(1 2 3)

CL-USER 10 > (position '(1 2 3) (list '(5 2) '(x y z) '(1 2 3) 'hello))
NIL

CL-USER 11 > (position '(1 2 3) (list '(5 2) '(x y z) obj 'hello))
NIL

CL-USER 12 > (position obj (list '(5 2) '(x y z) '(1 2 3) 'hello))
NIL

CL-USER 13 > (position obj (list '(5 2) '(x y z) obj 'hello))
2


Hope this helps you.
The MEMBER function also works this way.
Take a look at the hyperspec:
http://www.lispworks.com/reference/HyperSpec/Front/index.htm

http://www.lispworks.com/reference/HyperSpec/Body/f_pos_p.htm
(position)


Andr�
--
From: Padu
Subject: Re: Position of lists within lists
Date: 
Message-ID: <AdmdnTHgpKKYkEzdRVn2gg@iswest.net>
"Andr� Thieme" wrote
> POSITION works nearly like you think it would.
> The test on equality is done by comparing the address of the object
> inside the ram.
> Whenever you type something quoted it gets a new address.
> See here:
> CL-USER 9 > (setf obj '(1 2 3))
> (1 2 3)
> CL-USER 10 > (position '(1 2 3) (list '(5 2) '(x y z) '(1 2 3) 'hello))
> NIL
> CL-USER 11 > (position '(1 2 3) (list '(5 2) '(x y z) obj 'hello))
> NIL
> CL-USER 12 > (position obj (list '(5 2) '(x y z) '(1 2 3) 'hello))
> NIL
> CL-USER 13 > (position obj (list '(5 2) '(x y z) obj 'hello))
> 2

Got it... I figured that out from the results of my simple tests, so
basically what I'm looking for is a POSITION that runs the equality test
with EQUAL instead of EQ.

While I don't find a better solution, here's the provisory solution to do
the search (keep in mind that list size is very small, always = 12)... well,
it does what I want, but I need to be concerned with performance, since this
function will run inside my alpha-beta pruning algorithm to calculate
heuristics... however, since I have only two weeks to deliver this project
(damn summer courses!), I'll first try to finish it, then optimize it

    (dolist (element my-list)
      (when (equal e (list player-index pit-index))
        (return i))
      (incf i))

given that my-list is typically something like ((2 0) (2 1) (2 2) .... )
In this example, if player-index=2 and pit-index=1 then this dolist would
return 1 (because it found '(2 1))


> Hope this helps you.
> The MEMBER function also works this way.
> Take a look at the hyperspec:
> http://www.lispworks.com/reference/HyperSpec/Front/index.htm
> http://www.lispworks.com/reference/HyperSpec/Body/f_pos_p.htm
> (position)

Hey, that's much better than the "Common Lisp the Language" web site that
I'm using... thanks
I'm also using the wilensky book, but this hyperspec site is much more
complete.

Cheers

Padu
From: Howard Ding <······@hading.dnsalias.com>
Subject: Re: Position of lists within lists
Date: 
Message-ID: <m31xkeoodz.fsf@frisell.localdomain>
"Padu" <····@merlotti.com> writes:

> 
> Got it... I figured that out from the results of my simple tests, so
> basically what I'm looking for is a POSITION that runs the equality test
> with EQUAL instead of EQ.
> 

There is a POSITION that uses EQUAL instead of EQ.  It's called
POSITION. :-)  Just pass the test you want via the keyword argument
:test.


* (position '(1 2) '(a b (1 2) c))
; 
NIL
* (position '(1 2) '(a b (1 2) c) :test #'equal)

2

Using :test and :key (and :start and :end and so forth) will help you
get the most out of sequence functions.

-- 
Howard Ding
<······@hading.dnsalias.com>
From: Barry Margolin
Subject: Re: Position of lists within lists
Date: 
Message-ID: <barmar-D10CCC.23233016062004@comcast.dca.giganews.com>
In article <······················@iswest.net>,
 "Padu" <····@merlotti.com> wrote:

> Got it... I figured that out from the results of my simple tests, so
> basically what I'm looking for is a POSITION that runs the equality test
> with EQUAL instead of EQ.

Have you read the documentation for POSITION?  In particular, notice the 
:TEST option, which *all* the functions that search for things in 
sequences accept.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
From: Wade Humeniuk
Subject: Re: Position of lists within lists
Date: 
Message-ID: <wn9Ac.48466$Ds.13623@clgrps12>
Barry Margolin wrote:


> 
> Have you read the documentation for POSITION?  In particular, notice the 
> :TEST option, which *all* the functions that search for things in 
> sequences accept.
> 

Looking at the spec:

http://www.lispworks.com/reference/HyperSpec/Body/f_pos_p.htm

it does not directly define what :test defaults to.  I suppose
that is because of the existence of :test-not.

 From what I can glean it is specified that POSITION
uses eql by default at the bottom of this page:

http://www.lispworks.com/reference/HyperSpec/Body/f_eql.htm#eql

That seems a little too far away (too minimal) for a novice.

Wade
From: Tim Bradshaw
Subject: Re: Position of lists within lists
Date: 
Message-ID: <fbc0f5d1.0406170311.7f23af3e@posting.google.com>
Wade Humeniuk <········@telus.delete.net> wrote in message news:<····················@clgrps12>...

> 
> it does not directly define what :test defaults to.  I suppose
> that is because of the existence of :test-not.
> 

All the sequence functions default to EQL.
> 
> That seems a little too far away (too minimal) for a novice.

The spec is a language definition, not something aimed at novices. 
It's unfortunate, perhaps, that there isn't a good CL book which *is*
aimed at novices (although judging by recent announcements here it
looks like there may soon be one), but don't blame the spec for
that...

--tim
From: André Thieme
Subject: Re: Position of lists within lists
Date: 
Message-ID: <cas6gl$s9i$1@ulric.tng.de>
Wade Humeniuk schrieb:

> Barry Margolin wrote:
> 
> 
>>
>> Have you read the documentation for POSITION?  In particular, notice 
>> the :TEST option, which *all* the functions that search for things in 
>> sequences accept.
>>
> 
> Looking at the spec:
> 
> http://www.lispworks.com/reference/HyperSpec/Body/f_pos_p.htm
> 
> it does not directly define what :test defaults to.  I suppose
> that is because of the existence of :test-not.
> 
>  From what I can glean it is specified that POSITION
> uses eql by default at the bottom of this page:
> 
> http://www.lispworks.com/reference/HyperSpec/Body/f_eql.htm#eql
> 
> That seems a little too far away (too minimal) for a novice.

As a Lisp newbi the HyperSpec is not the easiest read for me. However, 
beeing in a similar position like Padu with 14 years of programming 
experience it is not an absolute mistery.
While learning more about Lisp I can find more and more information in 
the HyperSpec.


Andr�
--
From: Padu
Subject: Re: Position of lists within lists
Date: 
Message-ID: <WcadncKZmMFUWUzdRVn2jQ@iswest.net>
"Barry Margolin" wrote
> Have you read the documentation for POSITION?  In particular, notice the
> :TEST option, which *all* the functions that search for things in
> sequences accept.

Yes, I had read that, but only after some fellows here hinted me what is
that option for... the explanation of the :key parameter limits to "key---a
designator for a function of one argument, or nil.", but it doesn't try to
explain what that function is going to be used... I guess if I had thought a
bit more about it I'd have guessed.

Anyways, thanks everybody to point me the right direction.
From: Barry Margolin
Subject: Re: Position of lists within lists
Date: 
Message-ID: <barmar-FC5B7C.13480717062004@comcast.dca.giganews.com>
In article <······················@iswest.net>,
 "Padu" <····@merlotti.com> wrote:

> "Barry Margolin" wrote
> > Have you read the documentation for POSITION?  In particular, notice the
> > :TEST option, which *all* the functions that search for things in
> > sequences accept.
> 
> Yes, I had read that, but only after some fellows here hinted me what is
> that option for... the explanation of the :key parameter limits to "key---a
> designator for a function of one argument, or nil.", but it doesn't try to
> explain what that function is going to be used... I guess if I had thought a
> bit more about it I'd have guessed.
> 
> Anyways, thanks everybody to point me the right direction.

I believe there's a whole section at the beginning of the Sequences 
chapter that explains in detail what all these options are for.  It's 
not repeated in each dictionary entry, since it's the same for all the 
functions.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
From: André Thieme
Subject: Re: Position of lists within lists
Date: 
Message-ID: <cas6l5$s9i$2@ulric.tng.de>
Padu schrieb:

> "Andr� Thieme" wrote
> 
>>POSITION works nearly like you think it would.
>>The test on equality is done by comparing the address of the object
>>inside the ram.
>>Whenever you type something quoted it gets a new address.
>>See here:
>>CL-USER 9 > (setf obj '(1 2 3))
>>(1 2 3)
>>CL-USER 10 > (position '(1 2 3) (list '(5 2) '(x y z) '(1 2 3) 'hello))
>>NIL
>>CL-USER 11 > (position '(1 2 3) (list '(5 2) '(x y z) obj 'hello))
>>NIL
>>CL-USER 12 > (position obj (list '(5 2) '(x y z) '(1 2 3) 'hello))
>>NIL
>>CL-USER 13 > (position obj (list '(5 2) '(x y z) obj 'hello))
>>2
> 
> 
> Got it... I figured that out from the results of my simple tests, so
> basically what I'm looking for is a POSITION that runs the equality test
> with EQUAL instead of EQ.

Some people already pointed it out. You can use :test 'equal.
Btw, my second posting to this thread covered this ;-)
Okay, perhaps it was not so easy to see...


Andr�
--
From: Joe Marshall
Subject: Re: Position of lists within lists
Date: 
Message-ID: <n0323pyw.fsf@ccs.neu.edu>
"Padu" <····@merlotti.com> writes:

> Got it... I figured that out from the results of my simple tests, so
> basically what I'm looking for is a POSITION that runs the equality test
> with EQUAL instead of EQ.

If you can formulate what you need in this way, you will find that in
most cases someone thought of it and it's in there already.

The best counterexample, though, is CASE. 
From: André Thieme
Subject: Re: Position of lists within lists
Date: 
Message-ID: <car089$3o8$1@ulric.tng.de>
Padu schrieb:

> One more basic question.
> 
> If I have a given list (setq x '(a b c)) and I want to find the index of
> element 'b in that list, I know that position works well
> (position 'b x) => 1
> 
> now, imagine that my list is composed of tuples:
> (setq x '((1 0) (1 1) (1 2) (2 0) (2 1) (2 2)))
> and I want to find what is the position of element (1 2)
> I would use pos again,
> (position '(1 2) x) => NIL
> but it returns nil... am I using the wrong function or there's simply no way
> to do it using a single function (and therefore I'll have to create a small
> function for that)?

And one more thing about it...
Perhaps you know that CL supports several ways to check for equality.
In C you can check two strings with == or bytewise.
CL has for example EQ, EQL and EQUAL.
You can order POSITION to use EQUAL if you want:

CL-USER 19 > (eq '(1 2 3) '(1 2 3))
NIL

CL-USER 20 > (eql '(1 2 3) '(1 2 3))
NIL

CL-USER 21 > (equal '(1 2 3) '(1 2 3))
T

CL-USER 22 > (position '(1 2 3)
                        (list '(5 2) '(x y z) '(1 2 3) 'hello)
                        :test 'equal)
2


Andr�
--
From: Kenny Tilton
Subject: Re: Position of lists within lists
Date: 
Message-ID: <cI9Ac.192362$WA4.127336@twister.nyc.rr.com>
Padu wrote:

> One more basic question.
> 
> If I have a given list (setq x '(a b c)) and I want to find the index of
> element 'b in that list, I know that position works well
> (position 'b x) => 1
> 
> now, imagine that my list is composed of tuples:
> (setq x '((1 0) (1 1) (1 2) (2 0) (2 1) (2 2)))
> and I want to find what is the position of element (1 2)
> I would use pos again,
> (position '(1 2) x) => NIL
> but it returns nil... am I using the wrong function or there's simply no way
> to do it using a single function (and therefore I'll have to create a small
> function for that)?

Great question. Others have now filled you in on the :test option, but 
the meta-problem is getting used to the idea that Lisp so often DWIM. We 
  think we know about POSITION, but we do not: we came across a need for 
it to behave a certain way (equal vs eql) and we have not yet become 
accustomed to the fact that probably someone else along the way came 
across the same need and snuck it into the language, so we go looking 
for alternatives.

Fortunately the coolness of Lisp is consistent, so the next time you 
have a search function and you realize you are matching lists for their 
elements' identities, you will look for and discover it takes a :test 
option.

kenny

-- 
Home? http://tilton-technology.com
Cells? http://www.common-lisp.net/project/cells/
Cello? http://www.common-lisp.net/project/cello/
Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film
Your Project Here! http://alu.cliki.net/Industry%20Application
From: Padu
Subject: Re: Position of lists within lists
Date: 
Message-ID: <LKydnZQyAqeXWkzdRVn2uw@iswest.net>
"Kenny Tilton" wrote
> Great question. Others have now filled you in on the :test option, but
> the meta-problem is getting used to the idea that Lisp so often DWIM. We
>   think we know about POSITION, but we do not: we came across a need for
> it to behave a certain way (equal vs eql) and we have not yet become
> accustomed to the fact that probably someone else along the way came
> across the same need and snuck it into the language, so we go looking
> for alternatives.
>
> Fortunately the coolness of Lisp is consistent, so the next time you
> have a search function and you realize you are matching lists for their
> elements' identities, you will look for and discover it takes a :test
> option.
>
> kenny


I'm starting to enjoy this type of plasticity in the language... as I wrote
in another post, if I had paid more attention to the specs of POSITION, I'd
probably had guessed that (neither hyperspec or common lisp language specs
are much didactic, but that's expected from specs), but fact is I wasn't
expecting this flexibility out of the language... I think I'm still with the
traditional language paradigm in my head. Other languages also use
procedural types in arguments, but usually they let *you* do that... from
the top of my mind I don't remember of any usual function in C++ or Pascal
that gives this degree of relevance to procedural arguments.

Padu
From: Kenny Tilton
Subject: Re: Position of lists within lists
Date: 
Message-ID: <oelAc.122950$Nn4.27238044@twister.nyc.rr.com>
Padu wrote:
> I'm starting to enjoy this type of plasticity in the language... as I wrote
> in another post, if I had paid more attention to the specs of POSITION, I'd
> probably had guessed that ...

or see if your environment has a trick to show you the arguments to a 
function as you are typing, hopefully automatically ala AllegroCl which 
shows me (in the status line of the main tool bar) the args of a 
function as soon as I hit a space after typing in the name, or if i go 
back and just click at the end of the name of something typed previously.

kenny