[erlang-questions] missing function

Edward Stow ed.stow@REDACTED
Tue Dec 23 20:09:48 CET 2008


2008/12/23 Maxim Treskin <zerthurd@REDACTED>:
> Hello
>
> I can't find standard function which returns position of element in list
> like:
>
> num(El, List) ->
>     num(El, List, 1).
>
> num(_, [], _) -> {error, not_found};
> num(El, [H|T], Acc) ->
>     case H of
>         El -> Acc;
>         _ -> num(El, T, Acc+1)
>     end.

Hi,  I'm new to Erlang and FP and would like to ask why or when you
would prefer to use case expressions rather than function pattern
matching such as

indexOf(_,[],_) -> {error, not_found};

indexOf(El, [El | _], Index) -> Index;

indexOf(El, [_| T], Index) -> indexOf(El, T, Index + 1).

Intuitively I prefer the separate pattern matches as each match is
dealing with one case.

Does an idiom guide exist for Erlang / FP along the lines of Kent
Beck's 'Smalltalk Best Practice Patterns' ?

Thanks

-- 

Edward Stow



More information about the erlang-questions mailing list