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:

    A two-week-old infant cries an average of one and a half hours every day. This increases to approximately three hours per day when the child is about six weeks old. By the time children are twelve weeks old, their daily crying has decreased dramatically and averages less than one hour. This same basic pattern of crying is present among children from a wide range of cultures throughout the world. It appears to be wired into the nervous system of our species.
    Lawrence Kutner (20th century)

    Let us guard against saying that there are laws in nature. There are merely necessities: there is no one who commands, no one who obeys, no one who transgresses. Once you understand that there are no purposes, then you also understand that nothing is accidental: for it is only in a world of purposes that the word “accident” makes sense.
    Friedrich Nietzsche (1844–1900)