Strict functions

Andrew J Bromage andrew@bromage.org
Sat, 20 Oct 2001 01:11:05 +1000


G'day all.

On Fri, Oct 19, 2001 at 02:30:59PM +0100, Ian Lynagh wrote:

> Also, the prelude definition of zipWith has LVL whereas the following
> definition has LVV. Why is something like the following not used?
> 
> > zipWith                 :: (a->b->c) -> [a] -> [b] -> [c]
> > zipWith f (a:as) (b:bs) = f a b : zipWith f as bs
> > zipWith _ _      []     = []
> > zipWith _ _      _      = []

Generally speaking, Haskell programmers don't like inserting more code
with the only effect being to make the function less lazy.  This goes
double for standard library code.

I say "generally" because occasionally there's a good reason (e.g.
forcing evaluation can make a program more space-efficient).  Is there
a good reason behind your version of zipWith other than the strictness
signature being more symmetrical? :-)

If you really need a reason which doesn't involve bottom, consider a
(fairly common) call such as:

	zipWith f xs [1..]

If xs is finite, your version of zipWith would evaluate the infinite
list [1..] one place beyond that which was really needed.

Cheers,
Andrew Bromage