Command that gives index of element in list

A

Alan Rodricks

Guest
Hi

Is there a command that will give me the index of an element in a
list

Regards
Alan Rodricks
 
One of the possible solutions would be to convert that to an array and
then get the element with the given index

something like this,

a = list( 1 2 3)
listToVector(a)[0]

Partha
 
a = list( 1 2 3)
listToVector(a)[0]
easier would be to use nth(0 a) however i think the question was about the opposite.
i'll assume that.
i'm not aware of such a function, however it's rather easy to write one

procedure( indexof(x lst)
length(lst) - length(member(x lst))
)

since member returns the tail of the list starting at the given element.

cheers,
stéphane
 
I don't think there is a command to do this directly, so I always do this:

;; define procedure
procedure(getElemIndex(elem list)
let((foundList)
when(foundList = member(elem list)
length(list)-length(foundList)
)
)
) ; ** when foundList **

;; test and examine usage
a = list(1 2 3 4 5 6 7 8 9 1 2 3 4 5)

getElemIndex(1 a) => 0
getElemIndex(9 a) => 8
getElemIndex(10 a) => nil

Obviously, it only reports the index of the first match.

HTH,

Trevor

tattvamasi@gmail.com wrote:
One of the possible solutions would be to convert that to an array and
then get the element with the given index

something like this,

a = list( 1 2 3)
listToVector(a)[0]

Partha

Alan Rodricks wrote:
Hi

Is there a command that will give me the index of an element in a
list

Regards
Alan Rodricks
 
More efficient would be:

procedure(ABgetIndex(elem list)
let(((count 0))
exists(val list val==elem || count++ && nil) && count
))

This only has to traverse the list for as many items as don't match. Yours would
traverse the whole list twice in total. Mine would therefore take on average
1/4 of the list hops that yours would.

However, one thing to ask is why would you want to do this? Finding the index of
an entry in a list suggests that it is being treated as an array - when it is a
sequential structure. In other words, once you've found the index, what are you
going to do with it? If you're going to then use nth() etc to locate the element
later, that's not a good approach.

There may be a perfectly good usage for this, but you may well be approaching
the real problem the wrong way?

Regards,

Andrew.

On Thu, 25 Aug 2005 17:55:17 -0500, Trevor Bowen <m27315@gmail.com> wrote:

I don't think there is a command to do this directly, so I always do this:

;; define procedure
procedure(getElemIndex(elem list)
let((foundList)
when(foundList = member(elem list)
length(list)-length(foundList)
)
)
) ; ** when foundList **

;; test and examine usage
a = list(1 2 3 4 5 6 7 8 9 1 2 3 4 5)

getElemIndex(1 a) => 0
getElemIndex(9 a) => 8
getElemIndex(10 a) => nil

Obviously, it only reports the index of the first match.

HTH,

Trevor

tattvamasi@gmail.com wrote:
One of the possible solutions would be to convert that to an array and
then get the element with the given index

something like this,

a = list( 1 2 3)
listToVector(a)[0]

Partha

Alan Rodricks wrote:
Hi

Is there a command that will give me the index of an element in a
list

Regards
Alan Rodricks
 

Welcome to EDABoard.com

Sponsor

Back
Top