[erlang-questions] list_element()

karol skocik karol.skocik@REDACTED
Thu May 27 16:38:51 CEST 2010


lists:nth(1, [1,2,3]).

On Thu, May 27, 2010 at 4:31 PM, Henning Diedrich <hd2010@REDACTED> wrote:
> 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