TransWikia.com

The danger of pattern matching?

Mathematica Asked on January 31, 2021

enter image description here

I’m reading the book

Mathematica® programming: an advanced introduction by Leonid Shifrin

and there is a very nice evaluation. Here is:

This, plus a large number of quite generic and efficient built-in
higher-order functions (that is, functions that manipulate other
functions) allow for efficient general Mathematica programming
techniques. These techniques are not too difficult to learn, and in
some sense they split the entire Mathematica language into a
"scripting" (quick to write, but often slow to execute),
"intermediate" (a bit more thinking but faster code), and "system"
(less intuitive thinking, but yet much faster code) language layers

For example, the Pattern Matching style is elegant, short, but often slow.
Personally, I didn’t use often this type of Pattern Matching. For me, sometime it can cause some confusion.


Let’s take this example:

We have a list of data {{x,y},..}. We use the Pattern Matching to make a new list {{x,f[y]},..}. To do this, we can do:

list/.{a_, b_} -> {a, Log[b]}

However, someone can fall in the trap. For me, it’s rather dangerous.

If you have a list of 1 point. It works:

{{1, 2}} /. {a_, b_} -> {a, Log[b]}
{{1, Log[2]}}

If you have a list of 3 points. It works too:

{{1, 2}, {2, 4}, {3, 6}} /. {a_, b_} -> {a, Log[b]}
{{1, Log[2]}, {2, Log[4]}, {3, Log[6]}}

But for a list of 2 points, the logic has changed. It doesn’t work as intended.

{{1, 2}, {2, 4}} /. {a_, b_} -> {a, Log[b]}
{{1, 2}, {Log[2], Log[4]}}

My question:

I understand this behavior. But it takes me a bit of time to debug this. And I forget this type of "strange" behavior, and I often avoid to use Pattern Matching in Mathematica.

Do you have any example that using the Pattern Matching Style is faster than the classical Functional, or Procedural style?


Good read from Leonid’s book:

The part of the difficulty of learning Mathematica programming is
that there is no good formal distinction between these layers.

Typically, the first is characterized by heavy use of the procedural
(or otherwise straightforward) code, the second corresponds to use of
functional programming and the third by heavy use of optimized
structural operations, but this is not an absolute criteria. One and
the same operation can play a "scripting" role in one context and
"system" role in another.

For many problems (especially purely scientific), "scripting" layer is
sufficient. This layer consists mainly in using built-in commands or
gluing them with a typically procedural code. A big part of the bad
reputation that Mathematica used to have for its "slow performance" is
related to the fact that most people are only aware of this language
layer, because it corresponds most directly to their programming
experience in other (procedural) languages.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP