Guard (computer Science) - Pattern Guard

In addition to a guard attached to a pattern, pattern guard can refer to the use of pattern matching in context of a guard. In effect, a match of the pattern is taken to mean pass. This meaning was introduced by a proposal for Haskell by Simon Peyton Jones titled A new view of guards in April 1997 and has been further used about the implementation of the proposal. The feature provides the possibility to use patterns in the guards of a pattern.

An example in the extended Haskell:

clunky env var1 var2 | Just val1 <- lookup env var1, Just val2 <- lookup env var2 = val1 + val2 -- ...other equations for clunky...

This would read: "Clunky for an environment and two variables, in case that the lookups of the variables from the environment produce values, is the sum of the values. ..." Like in list comprehensions, the guards are in series, and if any of them fails the branch is not taken.

Read more about this topic:  Guard (computer Science)

Famous quotes containing the words pattern and/or guard:

    His talent was as natural as the pattern that was made by the dust on a butterfly’s wings. At one time he understood it no more than the butterfly did and he did not know when it was brushed or marred. Later he became conscious of his damaged wings and of their construction and he learned to think and could not fly any more because the love of flight was gone and he could only remember when it had been effortless.
    Ernest Hemingway (1899–1961)

    Although adults have a role to play in teaching social skills to children, it is often best that they play it unobtrusively. In particular, adults must guard against embarrassing unskilled children by correcting them too publicly and against labeling children as shy in ways that may lead the children to see themselves in just that way.
    Zick Rubin (20th century)