[erlang-questions] list_element()

Henning Diedrich hd2010@REDACTED
Thu May 27 16:50:03 CEST 2010


No, look the index up by the content.

That's a misleading title.

Ivan Uemlianin wrote:
> Dear Henning
>
> Do you mean you need to access the nth element of a list?  Is 
> lists:nth/2 no good?
>
>     http://www.erlang.org/doc/man/lists.html#nth-2
>
> Best wishes
>
> Ivan
>
> On 27/05/2010 15:31, Henning Diedrich 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