Channels ▼

Christopher Diggins

Dr. Dobb's Bloggers

Pattern Maching in Programming Languages

May 30, 2009

Several functional programming languages (e.g. Scala, Haskell, and ML) support pattern matching of data objects. Recently Martin Odersky, the designer of Scala, explained pattern matching. 

In a recent interview with Bill Venners at Artima.com Martin Odersky explained how pattern matching works in Scala, and what its role is. 

It is impossible to argue that pattern matching isn't useful to programmers. Especially to those writing compilers. But I don't agree as Martin says, that it is essential. I think he realizes this, and it was just a mild exageration. In the world of compiler writing, after you have gotten used to pattern matching, it is very painful to go back. 

I do however have one issue with pattern matching: the deconstruction of objects is coupled with the construction of the object. I don't believe that the consumer of data (e.g. objects) should care about how that data is produced or structured. Otherwise, we can't make changes to the production of the objects, without affecting the consumption. In other words: these concerns are coupled. 

So what should a language designer do? 

Well I am thinking that maybe we can have pattern matching, and decouple it from object construction and data layout by having objects expose a single function which returns a tuple representing the data layout to be matched on. 

So for example in an XML tree, an XML node might have a function with the signature "deconstruct() : List<XMLNode>" which could be used in pattern matching statements.

This way the actual layout of data in the XML node class, won't affect pattern matching. We are minimizing the point of contact between the pattern matching and the class. 

 The only other idea I have is introducing a type switch statement into the langauge. This would allow programmers to replace:

 if (x is A)
{
  f(x as A); 
else if (x is B)
{
   g(g as B);
 ...  

With the following code

typeswitch (x)
{
  case (A) :
    f(x);
    break;
  case (B) :
    g(x); 
    break; 
  ... 
 
Arguably this is a poor man's pattern matching, but maybe it is still an improvement over nothing, even if it isn't as powerful as Scala pattern matching.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video