Executing boolean expressions on a list

Richard Carlsson <>
Sat Mar 15 13:03:13 CET 2003

On Fri, 14 Mar 2003, Pierpaolo BERNARDI wrote:

> > 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.
> andalso should be good here.

Yes. However, it currently kills the tail recursion. The evaluation of
'andalso' checks that both arguments are booleans. With a bit of type
analysis, the check for the recursive case could be removed, making the
function tail recursive. The compiler will probably soon be able to do
this, but right now the resulting code is not tail recursive.

*However*: it is still an elegant way of writing the function, tail
recursive or not, so unless you *really* feel that you must optimize for
speed, I suggest that you use the 'andalso' version rather than
rewriting it.


Richard Carlsson ()   (This space intentionally left blank.)
E-mail: 	WWW: http://user.it.uu.se/~richardc/

More information about the erlang-questions mailing list