[erlang-questions] Additions to lists module

Serge Aleynikov saleyn@REDACTED
Wed Nov 26 14:30:19 CET 2008


Fixed a typo in foldlwhile.


%% @spec (List::list(), Ele) -> integer()
%% @doc Returns the position of `Ele' in the `List'. 0 is returned
%%      when `Ele' is not found.
%% @end
pos(List, Ele) ->
     pos(List, Ele, 1).
pos([Ele | Tail], Ele, Pos) ->
     Pos;
pos([_ | Tail], Ele, Pos) ->
     pos(Tail, Ele, Pos+1);
pos([], _Ele, _) ->
     0.

%% @spec (Pred, Acc, List::list()) -> Acc1
%%          Pred = (Ele, Acc) -> {boolean(), Acc1}
%% @doc A combination of foldl/3 and takewhile/2. Accumulate the result
%%      while the predicate function returns {true, _}.
%% @end
foldlwhile(Pred, Acc0, [H|T]) ->
     case Pred(H, Acc0) of
     {true, Acc} ->
         foldwhile(Pred, Acc, T);
     {false, Acc} ->
         Acc;
     end;
foldlwhile(_Pred, Acc, []) ->
     Acc.




More information about the erlang-questions mailing list