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

Anthony Clayden anthony.d.clayden at gmail.com
Fri Oct 1 03:06:45 UTC 2021


Browsing some docos for a completely other purpose, I came across this code:

>    f' [x, y] | True <- x, True <- y = True
>    f' _ = False

(In User Guide 6.7.4.5 Matching of Pattern Synonyms.)

That business with the comma and left-arrows? They're 'Pattern guards',
Language Report 2010 section 3.13. That also specs 'local bindings'
introduced by `let`.

In 10 years of reading Haskell code, I've never seen them. Does anybody use
them? Are they more ergonomic than guards as plain Boolean expressions? Are
'local bindings' any different vs shunting the `let` to the rhs of the `=`?

I'd write that code as:

>    f'' [x at True, y at True]  = True
>    f'' _ = False

I can see the rhs of the matching arrow could in general be a more complex
expression. But to express that you could put a more complex Boolean
guard(?)

AntC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20211001/c9889259/attachment.html>


More information about the Haskell-Cafe mailing list