Are pattern guards obsolete?

Neil Mitchell ndmitchell at gmail.com
Sun Dec 10 20:43:59 EST 2006


Hi

> > clunky env var1 var1 = case lookup env var1 of
> >   Nothing -> fail
> >   Just val1 -> case lookup env var2 of
> >     Nothing -> fail
> >     Just val2 -> val1 + val2
> > where
> >   fail = var1 + var2
> >
> >
> > Wouldn't
> >
> > > clunky :: Num a => [(a, a)] -> a -> a -> a
> > > clunky env var1 var2 = case (lookup var1 env, lookup var2 env) of
> > >         (Just v1, Just v2) -> v1 + v2
> > >         _ -> var1 + var2

The main advantage of pattern guards (to me at least) is that they can
appear on the LHS, so failing to match can result in fall through.
Everything else has to come on the RHS, meaning you have pick the RHS
by this point. This is bad because it now means two entirely separate
alternatives just got merged into one, reducing program readability.

I would love this to be in Haskell'. In fact, if we could only pick
one thing to go in Haskell', I would pick pattern guards. I would have
used pattern guards about 15 times today, if they were in Haskell.
Compare that to rank-2 types (never needed), MPTC (needed once ever)
etc. and you can see why I want this!

Thanks

Neil


More information about the Haskell-prime mailing list