2010-09-25

Short Intro of Mathematica For Lisp Programers (list processing example)

Short Intro of Mathematica For Lisp Programers (list processing example)

Xah Lee, 2008-08

The following is a exposition on the how programing language Mathematica is used for a list processing problem. The essay particulars details the nature of nested syntax in comparison to lisp. This article is originally posted to “comp.lang.lisp” newsgroup.

Cortez wrote:

I need to traverse a list of lists, where each sublist is labelled by a number, and collect together the contents of all sublists sharing the same label. So if I have the list -

I suspect that there is a more efficient and elegant way of doing this, however. Any suggestions welcome.

Brief background: this is part of a program I've written for reading data from SDIF files, a binary format which stores sound description data. The labelled lists represent partials in spectral analysis data (partial-index, time, frequency).

The //. {f___,x_[a__],x_[b__],l___} -> {f,x[a,b],l}
means use a pattern matching so that if ajacent elements has the same head, merge them into one.

The shortcut syntax for structural transformation used above is this:

myExpr //. myPattern -> myNewForm

The “myExpr” is any expression. The “myPattern” is any structural pattern (i.e. like regex except it work on list structures and datatypes). The “myNewForm” is like the regex's replacement string.

The syntax
myExpr //. myPattern -> myNewForm
can also be written in purely nested form, like this:

ReplaceRepeated[myExpr, Rule[myPattern, myNewForm]]

Now, here's some explanation on the the shortcut syntax used to match patterns:

_ means any single symbol. FullForm syntax is Blank[].

__ means any 1 or more symbols. FullForm syntax is BlankSequence[].

___ means any 0 or more symbols. FullForm syntax is BlankNullSequence[].

x_ is a syntax shortcut for Pattern[x,Blank[]], meaning it is a pattern, to be named “x”, and the pattern is Blank[]. We name the pattern so later you can refer to the captured element as “x”.

f___ is a syntax shortcut for Pattern[f,BlankNullSequence[]].
It means a pattern that matchs 0 or more elements, and any expression that matches can be later refered to as “f”.

The x_[a__] means basically a expression with 1 or more elements. The head we named “x”, and its elements we named “a”. FullForm syntax is Pattern[x, Blank[]][Pattern[a, BlankSequence[]]]. Similar for x_[b__].

So, all together, the {f___,x_[a__],x_[b__],l___} just matches a list and capture neighbors that has the same head.

The {f,x[a,b],l} just means the new form we want. Note the f,x,a,b,l are just names we used for the captured pattern.

which is another expression transformation with pattern. The _Integer[b___] just means any list who's head is of Integer type, and its element we name “b”. Any expression matching it is replaced by a list of its elements, expressed as {b}.

Now, all the above may seem like weired syntax. But actually it has a purely nested form.