[Haskell-cafe] Order of evaluation

Jon Harrop jon at ffconsultancy.com
Thu Jul 26 12:02:00 EDT 2007


On Thursday 26 July 2007 17:03:31 C.M.Brown wrote:
> Hi Jon,
>
> On Thu, 26 Jul 2007, Jon Harrop wrote:
> > If you have a boolean-or expression:
> >
> >   a || b
> >
> > will "a" be evaluated before "b" in Haskell as it is in other languages?
>
> Yes, I believe it is defined thus:
>
> True || _    = True
> _    || True = True
> _    || _    = False
>
> Therefore it is strict in its first argument (it needs to evaluate its
> first argument in order to know which pattern match to take).

Wonderful, thanks guys. The reason I ask is that I'm just looking over the 
Haskell ray tracer and it occurred to me that evaluation order makes an 
asymptotic difference to performance. The reason is simply that one order 
considers near spheres first and culls far spheres whereas the opposite order 
ends up traversing all spheres.

Do foldl and foldr reduce from the first and last elements of a list, 
respectively?

Specifically, I'm wondering if this has an effect on the foldr optimization 
that Spencer proposed (that certainly gives a ~50% speedup here) that was 
attributed to avoiding lazy accumulators, IIRC.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
OCaml for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists/?e


More information about the Haskell-Cafe mailing list