list_element()

Henning Diedrich hd2010@REDACTED
Thu May 27 16:31:00 CEST 2010


Hi list,

I need to access a list element by index.

I wrote a function but it looks most clumsy.

I am looking for maximal performance. Should I instead convert the list 
to a tuple and use element()?

The lists are ~ 10 to 30 elements in length. (<-- not an attempt to 
define a frame).

For curiosity, does the below tail-recurse correctly?

        listOrd(_, []) -> nil;

        listOrd(Searched, List) when is_list(List) ->

            listOrd(Searched, List, 1).

        listOrd(_, [], _) -> nil;

        listOrd(Searched, [ Element | Tail ], Count) ->

            case Searched == Element of
                true -> Count;
                _ -> listOrd(Searched, Tail, Count + 1)
            end.



Thanks for your time,
Henning


More information about the erlang-questions mailing list