[Haskell-cafe] Why isn't pattern matching lazy by default?

C.M.Brown cmb21 at kent.ac.uk
Wed Sep 19 14:43:14 EDT 2007


Hi Miguel,

> See, in let or where constructs you don't have a choice; you can't do
> different things depending on whether some value is Just x or
> Nothing. Therefore, there is no need to perform pattern matching
> strictly.

This is not entirely true. This is actually one of those niches in Haskell
where the left to right is not quite the same as right to left. A let can be converted to a where
but the other way round may require a case introduction.

So just like you can define:

f (Just x) = x
f Nothing  = error "Nothing"

You can also define:

f x = g x
       where
         g (Just x) = x
         g Nothing = error "Nothing"

g is strict in its first argument. Declared in a let it would look like:

f x = let g x = case x of
                   (Just y) -> y
                   Nothing -> error "Nothing"  in g x

Again, g must be strict in its first argument.

Chris.


> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list