MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Types in Mathematica

  • To: mathgroup at
  • Subject: [mg62258] Re: [mg62245] Types in Mathematica
  • From: "Virgilio, Vincent - SSD" <Vincent.Virgilio at>
  • Date: Thu, 17 Nov 2005 17:03:52 -0500 (EST)
  • Sender: owner-wri-mathgroup at

(Pardon the top-post.  MS Outlook requires it, I think.)

A recent event in the C++ community relates to this discussion
tangentially.  Bjarne Stroustrup and Gabriel Dos Reis (of GCC fame) just
published a formal definition (formalism) of the C++ language:

Have a look at the follow-up document as well,,

which uses the C++ formalism to specify C++ concepts.  For those new to
it, concepts have several uses, one being to constrain template

I don't think we should forget C++ in trying to understand Mathematica's
language.  I *bet* many illuminating comparisons can be made between the
pattern matching and expression evaluation mechanisms of Mathematica,
and the template-programming mechanisms of C++ (particularly those in
development, found at the above C++ working-group website).

Vince Virgilio

-----Original Message-----
From: Steven T. Hatton [mailto:hattons at]
To: mathgroup at
Subject: [mg62258] [mg62245] Re: Re: Types in Mathematica

Andrzej Kozlowski wrote:

>>> a=1;
>>> simply means whenever you see "a", replace it with "1". That's all.
>>> No type.
>> But there is IntegerQ.
> Yes, but
> f[(x_)?IntegerQ] := x^2
> a /: IntegerQ[a] = True;
> f[a]
> a^2


There are also conversion operators in C++ which cause an object of a
particular class to automatically be converted to a different type when
appropriate.  Additionally, one can overload operators.

>> One can view all symbols in Mathematica as variables in the sense of
>> computer science.
> All symbols? In what sense of symbol? How about:
> Head[Table]
> Symbol
> Is Table a "variable in the sense of computer science"?

It is an l-value.  And yes, in computer science a constant integer is a
"variable".  Furthermore, I can modify Table by unprotecting it, and
assigning to it.  Functions in Mathematica are not unlike functors in

>>> FORGET C++.
>> Why?  Just because Mathematica doesn't behave like C++, doesn't mean
>> there is no value in comparing the two languages.
> Undoubtedly, when you are beginner in Mathematica, knowing some other
> programming language can be of help. I remember that early versions of

> the Mathematica Book included several paragraphs in the introduction,
> each comparing Mathematica to a (then) better known programming
> language: I still recall that among these languages were C, Pascal,
> Lisp, and even Basic. However, once you get to a more advanced level
> thinking about other programming languages can be more of a hindrance
> than help.

Well, I certainly don't use a lot of for-loops, while-loops or other
procedural techniques when working with Mathematica. I tend to favor
Map, Apply and anonymous function.  That doesn't stop me from thinking
about functions as analogous to classes, and options and attributes as
analogs to "properties", i.e., member variables.

> The reason is that people coming form other programming languages tend

> to identify language with syntax. If they find that they can use
> familiar syntax they often believe they now know the language. But in
> the case of Mathematica the equation language = syntax does not hold.

The same could be said of C programmers who use C++.  They can write all
kinds of C-style code, and make absolutely no good use of most of C++'s

> Users notice this eventually when they discover that two Mathematica
> programs, both written using perfectly valid syntax, and both
> seemingly implementing the same algorithm (note the word "seemingly")
> can differ in performance by several orders of magnitude (if you want
> to see this really convincingly demonstrated just search for some past

> postings by Carl Woll). If then one understands by "knowing the
> Mathematica language" simply knowing how write programs that run, then

> it is true that anyone who knows C or LISP already knows quite a bit
> about Mathematica programming. But if by "knowing the Mathematica
> language" one means "knowing how to write efficient programs" than the

> advice "FORGET C++." needs to be only supplemented by the advice

I finally found what I was talking about in the Mathematica Book
regarding the use of the heads of expressions as "type tags":

"Expressions in Mathematica are often used to specify operations. So,
for example, typing in 2 + 3 causes 2 and 3 to be added together, while
Factor[x^6 - 1] performs factorization.

"Perhaps an even more important use of expressions in Mathematica,
however, is to maintain a structure, which can then be acted on by other
An expression like {a, b, c} does not specify an operation. It merely
maintains a list structure, which contains a collection of three
Other functions, such as Reverse or Dot, can act on this structure.

"The full form of the expression {a, b, c} is List[a, b, c]. The head
List performs no operations. Instead, its purpose is to serve as a "tag"
to specify the "type" of the structure.

"You can use expressions in Mathematica to create your own structures.
For example, you might want to represent points in three-dimensional
space, specified by three coordinates. You could give each point as
point[x, y, z]. The "function" point again performs no operation. It
serves merely to collect the three coordinates together, and to label
the resulting object as a point.

"You can think of expressions like point[x, y, z] as being "packets of
data", tagged with a particular head. Even though all expressions have
the same basic structure, you can distinguish different "types" of
expressions by giving them different heads. You can then set up
transformation rules and programs which treat different types of
expressions in different ways."
The Mathematica Wiki: Math for Comp
Math for the WWW:

This e-mail and any files transmitted with it are proprietary and intended solely for the use of the individual or entity to whom they are addressed. If you have received this e-mail in error please notify the sender. Please note that any views or opinions presented in this e-mail are solely those of the author and do not necessarily represent those of ITT Industries, Inc. The recipient should check this e-mail and any attachments for the presence of viruses. ITT Industries accepts no liability for any damage caused by any virus transmitted by this e-mail.

  • Prev by Date: Re: Magnetic Pendulum
  • Next by Date: Re: Magnetic Pendulum
  • Previous by thread: Re: Types in Mathematica
  • Next by thread: Re: Re: Types in Mathematica