Difference between revisions of "Pattern guard"

From HaskellWiki
Jump to navigation Jump to search
(Change category "Language" to subcategory "Language extensions")
(→‎See also: fix link to ghc user guide)
 
(9 intermediate revisions by 5 users not shown)
Line 1: Line 1:
The idea is to change the syntax for guards by replacing the use of a single condition with a list of qualifiers. These qualifiers, which include both conditions and ''pattern guards'' of the form ''pat <- exp'', serve to bind/match patterns against expressions. The syntax is comparable that of a [[list comprehension]], where instead the types of ''pat'' and ''exp'' match. This makes it easy, for instance, to pattern match against (possibly failing) table lookups while deciding which definition of a function to use.
+
Haskell 2010 changes the syntax for [[guard]]s by replacing the use of a single condition with a list of qualifiers. These qualifiers, which include both conditions and ''pattern guards'' of the form ''pat <- exp'', serve to bind/match patterns against expressions. The syntax is comparable that of a [[list comprehension]], where instead the types of ''pat'' and ''exp'' match. This makes it easy, for instance, to pattern match against (possibly failing) table lookups while deciding which definition of a function to use.
   
  +
From the GHC user's guide,
Stealing a function from the note,
 
 
<haskell>
 
<haskell>
  +
lookup :: FiniteMap -> Int -> Maybe Int
  +
 
addLookup env var1 var2
 
addLookup env var1 var2
 
| Just val1 <- lookup env var1
 
| Just val1 <- lookup env var1
Line 12: Line 14:
   
 
==See also==
 
==See also==
*[http://research.microsoft.com/Users/simonpj/Haskell/guards.html Simon Peyton Jones' note] on pattern guards
+
*[https://web.archive.org/web/20170109011924/http://research.microsoft.com/en-us/um/people/simonpj/Haskell/guards.html Simon Peyton Jones' note] on pattern guards
*[http://research.microsoft.com/%7Esimonpj/Papers/pat.htm Pattern Guards and Transformational Patterns].
+
*[https://www.microsoft.com/en-us/research/publication/pattern-guards-and-transformational-patterns/ Pattern Guards and Transformational Patterns].
  +
*{{GHCUsersGuide|exts/pattern_guards||a very short section on PatternGuards}}
  +
* [https://gitlab.haskell.org/haskell/prime/-/wikis/PatternGuards The Haskell Prime wiki page of Pattern Guards]
   
 
[[Category:Glossary]]
 
[[Category:Glossary]]

Latest revision as of 23:22, 24 July 2021

Haskell 2010 changes the syntax for guards by replacing the use of a single condition with a list of qualifiers. These qualifiers, which include both conditions and pattern guards of the form pat <- exp, serve to bind/match patterns against expressions. The syntax is comparable that of a list comprehension, where instead the types of pat and exp match. This makes it easy, for instance, to pattern match against (possibly failing) table lookups while deciding which definition of a function to use.

From the GHC user's guide,

lookup :: FiniteMap -> Int -> Maybe Int

addLookup env var1 var2
   | Just val1 <- lookup env var1
   , Just val2 <- lookup env var2
   = val1 + val2
{-...other equations...-}

will check to see if both lookups succeed, and bind the results to val1 and val2 before proceeding to use the equation.

See also