Erlang: the first item in the list that matches some condition (without evaluating the remaining items)

As a simple example, suppose I have a list of numbers L, and I want to find the first element that is larger than a certain number X. I could do this with a list of such concepts:

(mynode@127.0.0.1)24> L = [1, 2, 3, 4, 5, 6].              
[1,2,3,4,5,6]
(mynode@127.0.0.1)25> X = 2.5.
2.5
(mynode@127.0.0.1)26> [First | _] = [E || E <- L, E > X].  
[3,4,5,6]
(mynode@127.0.0.1)27> First.
3

But this seems potentially very inefficient, as the list can be very long, and the first match may be at an early stage. So I'm wondering if there is a) an effective way to do this that will not evaluate the remaining elements in the list after the first match is found? or b) When does this compile, does Erlang optimize the rest of the comparisons anyway?

Here's how I would achieve what I'm looking for in C:

int first_match(int* list, int length_of_list, float x){
    unsigned int i;
    for(i = 0; i < length_of_list, i++){
        if(x > list[i]){ return list[i]; } /* immediate return */
    }
    return 0.0; /* default value */
}
+5
3

, -

firstmatch(YourList, Number) -> 
   case lists:dropwhile(fun(X) -> X =< Number end, YourList) of
     [] -> no_solution;
     [X | _] -> X
   end.
+11

. , / ( , ).

-module(lazy_first).

-export([first/3]).

first(L, Condition, Default) ->
  first(L, [], Condition, Default).

first([E | Rest], Acc, Condition, Default) ->
  case Condition(E) of
    true -> E;
    false -> first(Rest, [E | Acc], Condition, Default)
  end;

first([], _Acc, _Cond, Default) -> Default.

:

14> lazy_first:first([1, 2, 3, 4, 5], fun(E) -> E > 2.5 end, 0.0).
3
15> lazy_first:first([1, 2, 3, 4, 5], fun(E) -> E > 5.5 end, 0.0).
0.0

Edit

.

first([E | Rest], Condition, Default) ->
  case Condition(E) of
    true -> E;
    false -> first(Rest, Condition, Default)
  end;

first([], _Cond, Default) -> Default.
+3

Here's a quick fix:

first_greater([],_) -> undefined;
first_greater([H|_], Num) when H > Num -> H;
first_greater([_|T], Num) -> first_greater(T,Num).
+3
source

All Articles