Executing boolean expressions on a list

Shawn Pearce <>
Sun Mar 16 03:14:23 CET 2003


Richard Carlsson <> wrote:
> 
> 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.


Double drat.  :-)

It was so pretty.  But I don't want the cost in here.  I don't think
the case somef(...) of true .. false .. end is that much uglier than
andalso, but it prevents me from chewing up stack space and saves
me a little bit.

I'm wrapping ets right now, and this code is sort of in the critical
path between ets and my caller.  ets performance was acceptable, so
I don't want it to get much worse.  Although the added functionality
is good....

In retrospect, I shouldn't have written this code and just used
mnesia.  I think.  ;-)

-- 
Shawn.

  Look afar and see the end from the beginning.



More information about the erlang-questions mailing list