[erlang-questions] Tail recursive tests

Per Melin <>
Wed May 28 17:52:03 CEST 2008


>From time to time I find that I want to write something like:

foo([]) -> true;
foo([H|T]) -> bar(H) andalso foo(T).

Which works, but is not tail recursive. Is there any reason why it couldn't be?

Does anyone consider it bad form? I find it clearer than:

foo([H|T]) ->
    case bar(H) of
        true  -> foo(T);
        false -> false
    end.

This is *not* another variation of the old "I hate Erlang's case and
if" discussion. I'm only talking about functions that evaluates to a
boolean.

Here's a more involved example:

search_for_key(<<>>, _, _) ->
    false;
search_for_key(B, C, K) ->
    delim_char(C) andalso subsearch(B, K)
        orelse
            <<C2, B2/binary>> = B,
            search_for_key(B2, C2, K).



More information about the erlang-questions mailing list