[Haskell-cafe] A tale of three shootout entries

Kalman Noel kalman.noel at bluebottle.com
Wed Nov 28 05:22:35 EST 2007


Simon Peyton-Jones wrote:
> You might think that unnecessary bangs shouldn't lead to unnecessary work --
> if GHC knows it's strict *and* you bang the argument, it should still only be
> evaluated once. But it can happen.  Consider
> 
>         f !xs = length xs
> 
> Even though 'length' will evaluate its argument, f nevertheless evaluates it
> too.

I'm replying to a guru here, so I should keep my voice low; but I'd like to
point out what might result in a misunderstanding for other readers of
haskell-cafe. Contrasting both the bang pattern and the usage of length causing
f to be strict, one might suppose that a bang pattern, when used on a list, will
cause it to be evaluated in the same way as length does. However,

> the *first* thing length does is evaluate its argument,

and it will furthermore evaluate the argument list recursively, as much as is
necessary to determine its length. On the other hand, given

    g !xs = ()

evaluating g [0..] will terminate, because g is only strict in the constructor
of its argument, which is (:). The list data type itself, however, is
non-strict.

Kalman

----------------------------------------------------------------------
Free pop3 email with a spam filter.
http://www.bluebottle.com/tag/5



More information about the Haskell-Cafe mailing list