Executing boolean expressions on a list
Peter H|gfeldt
peter@REDACTED
Fri Mar 14 03:29:49 CET 2003
How about lists:all/2,
recursive(N, Es) ->
lists:all(fun(E) -> somef(N, E) end, Es) ?
/Peter
On Thu, 13 Mar 2003, Shawn Pearce wrote:
> I find I'm writing a lot of:
>
> recursive(N, [H | T]) ->
> case somef(N, H) of
> true -> recursive(N, T);
> false -> false
> end;
> recursive(N, []) ->
> true.
>
> somef(N, E) ->
> .... % return true or false.
>
> Is there a faster way to write recursive/2 ? In some cases I have to hand
> around 2 or 3 args instead of just N, but usually they are just payload
> data to somef/2 (or whatever arity) to help it perform its test. I'm
> just wondering if perhaps there's a faster way to express the case .. end
> in recursive/2.
>
> I'm trying to keep it tail-recursive. I thought about:
>
> recursive(N, [H | T]) ->
> if
> somef(N, H) -> recursive(N, T);
> false -> false
> end;
> recursive(N, []) -> true.
>
> which I didn't like because of the formatting of the if statement, and:
>
> recursive(N, [H | T]) -> somef(N, H) and recursive(N, T);
> recursive(N, []) -> true.
>
> but was worried about order of evaluation on the and operator
> and not being able tail-recursive.
>
>
> Feedback from the Erlang pros is good. ;-)
>
> --
> Shawn.
>
> Q: Minnesotans ask, "Why aren't there more pharmacists from Alabama?"
> A: Easy. It's because they can't figure out how to get the little
> bottles into the typewriter.
>
More information about the erlang-questions
mailing list