[erlang-questions] Additions to lists module

Peter Lund <>
Thu Nov 27 10:23:13 CET 2008


Yes I agree it is a bit of a "hack", but I think there are some people 
out there using it
this way already and this means the function of string:str/2 should not 
change in the
future since it should be backward compatible.

Better would be to add in the OTP module 'lists' something like this:

-module(lists).

find(List, Elem) -> string:str(List,[Elem]).

And document it properly for all new erlang users out there. It takes a 
while before
newbies discover this "hack" way of using it and it then lessens the 
good impression
of Erlang/OTP, so it would be much better to have this documented 
*properly* in
the lists module.

/ Peter

Serge Aleynikov skrev:
> Don't you agree that this is a hack given documented definition of the 
> function:
>
>     str(String, SubString) -> Index
>     rstr(String, SubString) -> Index
>
>     Types:
>         String = SubString = string()
>
> Serge
>
> Peter Lund wrote:
>> 1> string:str([x,y,s,my_arbitrary_erlang_term], 
>> [my_arbitrary_erlang_term]).
>> 4
>> 2>
>>
>>
>> Serge Aleynikov skrev:
>>> I'd like to propose addition of these two functions to the lists 
>>> module.   Very often I find the need for this functionality and feel 
>>> that it belongs to the standard library.
>>>
>>> Serge
>>>
>>>
>>>
>>> %% @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) -> 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(Fun, Acc, T);
>>>      {false, Acc} ->
>>>          Acc;
>>>      end;
>>> foldlwhile(_Pred, Acc, []) ->
>>>      Acc.
>>> _______________________________________________
>>> erlang-questions mailing list
>>> 
>>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>>
>>>   
>>
>>
>




More information about the erlang-questions mailing list