[Haskell-cafe] Pattern guards seen in the wild?

Anthony Clayden anthony.d.clayden at gmail.com
Fri Oct 1 10:48:29 UTC 2021


Thank you all for the examples.

Hehe real-world data structures are messy. Especially if dictated by
systems utilities that don't care for functional programming.

In the back of my mind is: do these idioms date from before there were
pattern synonyms? Would matching to a pattern synonym give clearer code in
the case branching?

(Would it be worth declaring a synonym for just one usage?)

There's only one of those examples where I feel expressing without the
Pattern guard would be awkward. Tom's example 1., whose characteristics are:

* There's a long straggly sequence of matchers;
* With a particularly awkward branch in the middle, needing sub-analysis of
a _component_ of the scrutinee, and only if such a component is present;
* If the sub-analysis fails to match, the logic must unnest itself and
resume matching at the next branch of the straggle.

The awkward branch (slightly reformatted) is

>                (GET, [x]) | Just (path', mime) <- List.lookup x staticFiles

>                             -> Just (StaticFile mime path')


`staticFiles` is a module-wide variable, so matching is looking
outside the scrutinee, therefore couldn't be expressed as a pattern
synonym. The `lookup` might return a `Nothing`, so the inner pattern
match could fail/this is doing more than a 'local binding' with `let`,
which "always succeed" says the Language Report.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20211001/6a691850/attachment.html>


More information about the Haskell-Cafe mailing list