[erlang-questions] To name a function

Attila Rajmund Nohl <>
Thu Dec 16 11:31:06 CET 2010


2010/12/16, Morten Krogh <>:
> Hi
>
> As soon as you have seen both an element that satisfies the condition
> and one that doesn't, you can return "some", making a full list
> traversal overkill for many inputs.

Now that's a good idea:

has_property(Fun, [E | Rest]) ->
    has_property(Fun, Fun(E), Rest).

has_property(_Fun, Prev, []) ->
    case Prev of
	true -> all;
	false -> none
    end;

has_property(Fun, Prev, [E | Rest]) ->
    Curr = Fun(E),
    case Curr of
	Prev -> has_property(Fun, Prev, Rest);
	_ -> some
    end.


More information about the erlang-questions mailing list