Pattern Matching: Comparing Elixir and Mathematica

Written by on August 16, 2020 in Elixir, Mathematica, Programming with 0 Comments

One of the things I like about Elixir is its support for patterns at the core language level, not through library functions as in most other languages. This contributes to writing cleaner code, in my opinion.  

Another environment that I am familiar with, namely Mathematica, boasts of (arguably) the most powerful symbolic programming language called “Wolfram Language”. Mathematica is primarily used in mathematical and scientific programming. Patterns and Rules are all-pervasive in this language.

It is not fair to compare these two languages since each addresses a different kind of problem. Elixir is meant for highly scalable and fault tolerant general-purpose computing, whereas Mathematica excels in complex symbolic (and numeric) computations, and over the years has been extended to include Neural Networks, Machine Learning, Natural Language Understanding, and so on.

In this article, I am going to compare the pattern matching ideas supported by Elixir and Mathematica.  Even this is a vast topic to address, so I will limit the discussion to the most striking aspects of pattern matching. 

In the following sections, when I use the term “Mathematica”, I actually mean “Wolfram Language”. 

The “=” Operator

Let us start with a simple idea. In Elixir, the operator “=” is called “Pattern Matching” operator, not an “Assignment Operator”, although the operation can involve assignment. Look at the following example:

Pattern Matching Operator

Pattern Matching Operator

Here, the two patterns will match only if X, Y, andare bound respectively to 1, 2 and 3. This causes the values to be bound to the variables. The operator, in this case, behaves like the assignment operator.

Here is a slight variation:

Pattern Matching (Contd.)

Pattern Matching (Contd.)

In this case, the first value of the LHS tuple is a constant and hence for the pattern to succeed, the RHS must have the same first element. The match succeeds since this is true, and Y and Z are bound as before.

What happens if there is a pattern mismatch? The following example shows this.

Pattern Mismatch

Pattern Mismatch

I hope you understand what is going on. There are other interesting variations of the pattern matching operator in Elixir, but we will not get into those here.

Coming to Mathematica, the “=” operator is the traditional “Assignment” operator. No pattern matching is occurs here. The following example is similar to what we tried in Elixir:

Assignment in Mathematica

Assignment in Mathematica

There is no surprise here since it merely involves assignment to the 3 variables on LHS. Now consider this:

Invalid Assignment

Invalid Assignment

Since we cannot assign to a constant on the LHS, the above fails. There is no attempt to “pattern match” in this case.

As the above examples show, the “=” operator in Elixir is more general than Mathematica’s assignment operator.

Case Statement

Elixir differs from most other programming languages in that it supports patterns (instead of simple conditions) in the “case” statement. Here is an example:

Case Statement in Elixir

Case Statement in Elixir

Notice how we use a “guard” along with the first pattern to check if the second element of the list is greater than 10. The second pattern matches any element in the second position of the list. The third pattern only checks if the expression is a list without considering its internal structure. The last condition will match anything that hasn’t matched the earlier three patterns. In this case, the given list “[1, 11, 3]” matches the first pattern.

Here is the same code as above but with the expression “[1, 9, 3]”. It matches the second pattern.

Case Statement (Contd.)

Case Statement (Contd.)

What is the result if we pass a 4-element list instead of a 3-element list? The third “qualified” pattern matches.

Case Statement (Contd.)

Case Statement (Contd.)

Finally, we pass a “string” instead of a list. In this case, the “catch all” pattern matches!

Case Statement (Contd.)

Case Statement (Contd.)

Mathematica uses “Switch” instead of “case”. Otherwise, the structure is quite similar. The following examples correspond to the above Elixir examples:

Mathematica Switch Statement

Mathematica Switch Statement

The operator “/;” puts constraints on the associated pattern. The following matches the second condition.

Switch Statement (Contd.)

Switch Statement (Contd.)

The following matches the third condition, a pattern that matches any “List”.

Switch Statement (Contd.)

Switch Statement (Contd.)

The last case corresponds to a “non-List” expression.

Switch Statement (Contd.)

Switch Statement (Contd.)

As you can observe, the behaviour is quite similar to Elixir.

Patterns and Guards in Functions

Both Elixir and Mathematica allow “guards” (or conditions) to be associated with function definitions. This is quite powerful. Let us go through an example to see how this is done.

Suppose that we want to compute the nth “Fibonacci” number. Here is one way to do this in Elixir:

Computing Fibonacci Number

Computing Fibonacci Number

Instead of coding the different trivial cases in a single function, we split the logic across multiple definitions. Notice also, how we are able to associate a “guard” or condition with the third definition to check if the passed argument is an integer (we take for granted that the argument is positive). 

Here is yet another way. We have merged the first two definitions by introducing a “guard”.

Fibonacci (Contd.)

Fibonacci (Contd.)

It is important to keep in mind that Elixir tries the functions in the order of definition. So, if we change the order as in the following case, it will not work.

Invalid Definition Order

Invalid Definition Order

How do we write this function in Mathematica? The following implementation corresponds to the first version we wrote in Elixir.

Fibonacci Function in Mathematica

Fibonacci Function in Mathematica

Mathematica too allows us to use “conditions” as part of function definition. The code given below matches the second version we wrote in Elixir.

Fibonacci (Contd.)

Fibonacci (Contd.)

A nice thing about Mathematica is that there is no restriction on the order in which the definitions must be provided. So, even the following will work correctly.

Order is not Important

Order is not Important 

Arbitrary Pattern-based Transformations in Mathematica

Unlike Elixir, Mathematica allows us to define functions that work with symbolic expressions. Take a look at the following snippet:

Symbolic Expressions

Working with Symbolic Expressions

The first line above defines a function that takes two arguments. The first argument can be anything as denoted by the pattern “x_”. The second argument must be an expression of the form “x_ raised to the power of anything”. Given two arguments like this, this function applies another function “p” to 2 times “x” (remember “x” refers to the first argument).

The second and third lines show what happens when this function is called with “(m+n)” and “(m+n) 3. The fourth and fifth lines show another valid invocation example. In both these cases, the argument structure is maintained and hence the expected transformation takes place. The last two lines show what happens when the second argument does not satisfy the required structure (it uses “n” instead of “m”).

As the final example of pattern transformations, Mathematica has a special “substitution” operator “//.” that can be applied to an arbitrary expression to transform it into another expression. Here is an example:

Substitution Operator

Substitution Operator

Here, the expression to the left of “//.” Is “X2 + Y2 + Z”. To the right of “//.” we have a list of substitutions to apply. The substitution operator repeatedly applies the given values to the LHS expression until it changes no more. Finally, we get the answer “16”.

Of course, the substitution can also be symbolic. Look at this:

Symbolic Substitution

Symbolic Substitution

Here we transform one symbolic expression to another. Such transformations are quite common in the domain of Mathematica.

In my view, Mathematica’s pattern matching capabilities are richer and more powerful than Elixir. What I have covered is just a tiny part of what Mathematica allows. 

Hope you enjoyed reading this article. Have a nice weekend and a wonderful week ahead!

Tags: , ,

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published.

Top