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

C.M.Brown cmb21 at kent.ac.uk
Wed Sep 19 07:54:39 EDT 2007


Hi Peter,

> Mmm, yes of course... blush...
>
> But shouldn't
>
> f ~(x:xs) = rhs
>
> give a compile-time error since neither x nor xs is used in the right
> hand side, and hence nothing will ever get pattern matched when this
> function is called, which clearly indicates a mistake? That is, if I
> understand lazy pattern matching correctly... And then in these cases
> the user would have to annotate the pattern match as being strict, so he
> is aware of the eager evaluation taking place

Well if you defined f:

f ~(x:xs) = 1 + 2
f ~[] = 42

Then you will get a warning stating that the pattern matches of (x:xs)
and [] are overlapped. It may not be a mistake though, so possibly a bold
error for the compiler to throw, it just means that 1+2  will always be
evaulated no matter what list you throw at it (provided of course that
the result of f is needed to evaluate the rest of the program).

It's interesting to note that if you had:

f ~(x:xs) = x + 2
f ~[] = 42

Then f [] would give a complie error:
 Irrefutable pattern failed for pattern (x : xs)

Hope that gives some insight.
Chris.


>
> Oh well, the way it is now is also easy to get used to, one just has to
> know how it works (just like M-theory ;-) )
>
> Cheers,
> Peter
>
>
> Neil Mitchell wrote:
> > Hi
> >
> >
> >>>  Now why isn't pattern matching lazy by default?  This seems odd for a
> >>> newbie since everything else is lazy by default.
> >>>
> >
> > f ~(x:xs) = rhs
> > f ~[]     = rhs'
> >
> > Now guess what f [] does...
> >
> > If you use a where binding then pattern matching is lazy.
> >
> > Thanks
> >
> > Neil
> >
> >
> >
>
>


More information about the Haskell-Cafe mailing list