[Haskell-cafe] haskell in online contests

wren ng thornton wren at freegeek.org
Sat Nov 28 21:47:52 EST 2009

vishnu wrote:
> I always thought lazyness was automatic and
> seq made strictness possible.

Laziness is the default, but that doesn't mean it's everywhere. For 
example, every time you do pattern matching you enforce strictness:

     foo (x:xs) = 5
     foo _|_ == _|_

However, there's also a way to make patterns lazy:

     foo ~(x:xs) = 5
     foo _|_ == 5

The difference here is in how they're compiled/desugared, something like 

     foo (x:xs) = 5
     foo = \xs' -> case xs' of (x:xs) -> 5

     foo ~(x:xs) = 5
     foo = \xs' -> let (x:xs) = xs' in 5
     foo = \xs' -> 5

So writing the "same" function in different ways can enforce different 
strictness behavior.

There are also more substantial algorithmic uses of laziness. One 
example is writing functions in a way that facilitates list fusion or 
on-line consumption of lists, vs writing functions in a way that forces 
more than one cell of the list at a time. Deforestation and garbage 
collection are not laziness, but appropriate uses of laziness can allow 
you to leverage them to improve the performance of your code.

Live well,

More information about the Haskell-Cafe mailing list