[erlang-questions] How do I elegantly check many conditions?

Adam Kelly <>
Fri Mar 20 17:47:23 CET 2009


2009/3/20 Attila Rajmund Nohl <>:
> 2009/3/20, Adam Kelly <>:
>> 2009/3/20 ryeguy <>:
>>> Now I don't want to have a 5 level deep if or case statement, but what
>>> other options do I have? Splitting it into separate functions sounds
>>> like a good idea, but then I just have to check the return value of
>>> the functions in some sort of conditional and it's back to the
>>> original problem.
>>
>> I've added this function to my libraries:
>>
>> run_checks([]) -> ok;
>> run_checks([H|T]) when is_function(H, 0) ->
>>       case H() of
>>               ok    -> run_checks(T);
>>               Error -> Error
>>       end.
>
> Something like this is badly missing from the lists module...

I'm not sure I'd put it in the lists module, maybe an additional
control structure
module would be good.  I have a collection of higher order functions for these
sorts of tasks.

On the other hand I'm partially convinced that the try -> catch badmatch
solution is neater anyway.

(I presume your response was intended for the list.)

Adam.



More information about the erlang-questions mailing list