Pattern matching is an overloaded term. Do you mean pattern matching the language feature, as found in Haskell or Scala, used to break data structures apart? If so, it is useful for pulling apart a structured entity in a syntactically clean way that resembles (sometimes exactly matches) the syntax used to create the entity.
Haskell in particular uses this technique very effectively almost everywhere. A quick google for Haskell Pattern Matching will give excellent examples. Here's one from the wikibook:
dropThree ([x,y,z] ++ xs) = xs
This defines a function that drops the first three items from a list. It takes one argument, the list, which it matches to the pattern of three list items [x,y,z] concatenated with the rest of a list xs. It returns (is equal to in Haskell syntax) xs. So in effect, you use the syntax to create a list with three items prepended to pull three items off instead by putting the syntax in the argument field. Very cool!
Is there a particular language you are looking at that caused you to ask? The particulars vary.
Show differencesHistory of post edits
#1Koobs
Posted 23 January 2012 - 02:05 AM
Pattern matching is an overloaded term. Do you mean pattern matching the language feature, as found in Haskell or Scala, used to break data structures apart? If so, it is useful for pulling apart a structured entity in a syntactically clean way that resembles (sometimes exactly matches) the syntax used to create the entity.
Haskell in particular uses this technique very effectively almost everywhere. A quick google for Haskell Pattern Matching will give excellent examples. Here's one from the wikibook:
dropThree ([x,y,z] ++ xs) = xs
This defines a function that drops the first three items from a list. It takes one argument, the list, which it matches to the pattern of three list items [x,y,z] concatenated with the rest of a list xs. It returns (is equal to in Haskell syntax) xs. So in effect, you use the syntax to create a list with three items prepended to pull three items off instead by putting the syntax in the argument field. Very cool!
Haskell in particular uses this technique very effectively almost everywhere. A quick google for Haskell Pattern Matching will give excellent examples. Here's one from the wikibook:
dropThree ([x,y,z] ++ xs) = xs
This defines a function that drops the first three items from a list. It takes one argument, the list, which it matches to the pattern of three list items [x,y,z] concatenated with the rest of a list xs. It returns (is equal to in Haskell syntax) xs. So in effect, you use the syntax to create a list with three items prepended to pull three items off instead by putting the syntax in the argument field. Very cool!