![]() |
![]() |
|
![]() |
![]() |
Encyclopedia :
P :
PA :
PAT :
Pattern matching |
|
|
Pattern matchingPattern matching is a feature in functional programming languages such as Haskell and ML. With certain data types implemented in these programming languages, such as a list, the construction of these types form a very definite structure. For example a list is defined as an element constructed on to an empty list, or an element constructed on a list. In Haskell syntax: The structure is thus we assert that the first element of In certain pattern matching, we may wish to disregard a certain part of the pattern, since we have no real use ascribing a name to it. In the example If such data does not match a pattern, an error inevitably occurs, or another subfunction with a pattern that does match this data is called to handle this data. Often this is the case for the empty list, as an empty list does not have a head (the first element that is constructed). Pattern matching failure can often be ascribed to a type signature that is not strong enough or is incorrect. Pattern matching is not limited to mere lists, but to user-defined data types. In Haskell, the following code segment defines a data type The keyword When we want to write functionss to make If we pass a variable that is of type Color, how can we get the data out of this variable? Such programming languages aforementioned use pattern matching to do so. For example, for a function to get the integer part of Or conversely:
|
|
|
This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License. |
|
| © 2008 Chamas Enterprises Inc. |