[erlang-questions] Getting the position of a list item
Wed Dec 2 19:00:35 CET 2009
On Wed, Dec 2, 2009 at 11:53 AM, Igor Ribeiro Sucupira <igorrs@REDACTED> wrote:
> I happened to need that once and ended up implementing my own, so I
> think you won't find it in the standard library.
> Later, I realized there was a more elegant way of doing what I wanted,
> so I should agree with Robert in that you probably don't need that
> functionality (but we could be wrong, of course...).
> find_first(Element, List) when is_list(List) ->
> find_first(Element, List, 0).
> find_first(_Element, , Inc) when is_integer(Inc) ->
> Inc + 1;
> find_first(Element, [Element | _Tail], Inc) when is_integer(Inc) ->
> Inc + 1;
> find_first(Element, [_ | Tail], Inc) when is_integer(Inc) ->
> find_first(Element, Tail, Inc + 1).
Yup, not hard to roll your own on this. I was just surprised it wasn't there.
For the record, I think lists should have:
lfind(Elem, List) -> N | false
rfind(Elem, List) -> N | false
But if a 20 year old language can get by without these, then by
definition *I'm* wrong :)
Still, I'm happy to contribute a patch for these ;)
More information about the erlang-questions