[erlang-questions] list_element()

Henning Diedrich hd2010@REDACTED
Thu May 27 22:19:42 CEST 2010


  Yes, thanks a lot.

Starts to look acceptable.

Actually

listOrd(Searched, [ Searched | _ ], Count) -> 
Count;
listOrd(Searched, [_ | Tail ], Count) ->
listOrd(Searched, Tail, Count+1).

Henning


Ariel Gomez wrote:
> you do not need to use "if", use pattern matching.
>
> ..
> ..
> listOrd(Searched, [ Searched | Tail ], Count) -> 
> Count;
> listOrd(Searched, [_ | Tail ], Count) ->
> listOrd(Searched, Tail, Count+1).
>
>
> 2010/5/27 Henning Diedrich <hd2010@REDACTED 
> <mailto:hd2010@REDACTED>>
>
>     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