lists:partition/2

Robert Virding rv@REDACTED
Thu Jan 18 10:57:25 CET 2001


Luke Gorrie <luke@REDACTED> writes:
>And here are a few other functions I've written lots of times:
>
>  %% For 'lists'
>  %% separate(X, L):
>  %% Separate the elements of the list L by placing X between them.
>  %% e.g.: lists:flatten(separate(", ", ["foo", "bar", "baz"]))
>  %%         => "foo, bar, baz"
>  separate(X, [])    -> [];
>  separate(X, [H])   -> [H];
>  separate(X, [H|T]) -> [H,X|separate(X, T)].
>
>  %% For 'lists'
>  %% first(Pred, List)
>  %% Find the first element of List satisfying Pred.
>  %% Returns: {value, Item} | false
>  first(Pred, []) ->
>      false;
>  first(Pred, [H|T]) ->
>      case Pred(H) of
>          true ->
>              {value, H};
>          false ->
>              first(Pred, T)
>      end.

The name is not 'first' is not very good to use for such a function.
There is a function pair hd/tl (head/tail) which return the first
element of a list and the other elements.  There is also a function
'last' which returns the last element of a list.  To have some form of 
name symmetry the function which returns all the elemnts except for the 
last should be called 'first'.

There has been some wimpish grumbling that a name like 'first' is not 
very obvious for a function like that.  The has been suggestions like 
'but_last'.  Can't understand the problem myself. :-)  Anyway many 
other functional libraries have 'first'.

	Robert





More information about the erlang-questions mailing list