# How to systematically classify Mathematica expressions?

How to systematically classify Mathematica expressions? I can think of using Head[], Depth[], Length[], and some special pattern based on the problems at hand. What other key words, or functions should I consider?

## Update

I mostly want to group symbols by how nested its list are, and what kinds of elements the lists have. For example

{_String, _Symbol}

{{_Integer}, _String}

_String


would be considered three distinct types.

-
This one seems too broad ... to classify for what? – Dr. belisarius Jan 17 '13 at 22:51
For example: Is this useul to you? Tally[Length /@ Characters@Names[]] – Dr. belisarius Jan 17 '13 at 22:55
It sounds as though the questioner wants to define the set of features which can be used to uniquely resolve Mathematica expressions into a set of classes. A definition of the set of classes would be helpful. – image_doctor Jan 17 '13 at 23:59

In your updated example, you would find the "classification" using your scheme by replacing the lowest level elements with a pattern based on their heads:

classify[expression_] := Map[Blank[Head[#]] &, expression, {-1}]


Then you can apply this on template examples of the patterns you listed:

a = {"string", symbol};
b = {{42}, "string"};
c= "string";

classify[a]
classify[b]
classify[c]
(* {_String, _Symbol} *)
(* {{_Integer}, _String} *)
(* _String *)

-

Did you considered looking at the book from S. Wolfram a new kind of science. There he discuses some main principals and rules applied to mathematica in particular Chapter 11: The Notion of Computation and further chapters.

-
Could you summarize that chapter for those who haven't read it yet? – Artes Jan 18 '13 at 0:38
I would claim that you cannot learn anything about Mathematica expressions from that book. I own the hard copy because my kids like the patterns. – Jens Jan 18 '13 at 0:39

The function Depth will separate the examples of the expressions you give into three distinct classes.

exps = {{_String, _Symbol}, {{_Integer}, _String}, _String}
Depth /@ exps


{3, 4, 2}

-
Well, Depth only gives the maximum depth, so it's not indicative of the structure of the expression. For instance, Depth@{{_Integer}, {_String}} is also 4, but it's different from the second example above. For deeper depths, there will be several possible combinations of nestings that will give the same value – R. M. Jan 18 '13 at 19:10
Yes absolutely true, in as much as it correctly classifies the three examples given it accurately separates the example data. Adding the set of contained heads, or a positionally ordered list of heads , would lead to a more refined classification. Or adding something like Belisarius suggests, a hash based on name length would also be an improvement. But to know which would suit best would probably require a more refined question. – image_doctor Jan 18 '13 at 20:35