MathGroup Archive 2005

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

Search the Archive

Re: Re: Types in Mathematica

  • To: mathgroup at
  • Subject: [mg62245] Re: Re: Types in Mathematica
  • From: "Steven T. Hatton" <hattons at>
  • Date: Thu, 17 Nov 2005 04:15:43 -0500 (EST)
  • References: <> <> <dl8s4g$n41$> <dl980q$r2a$> <> <dlc96b$m81$>
  • Sender: owner-wri-mathgroup at

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 C++.

>>> 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 functions.
An expression like {a, b, c} does not specify an operation. It merely
maintains a list structure, which contains a collection of three elements.
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 Sci
Math for the WWW:

  • Prev by Date: General--Interpolation and NonlinearFit
  • Next by Date: Re: Notebook for Borel-Tanner Distribution
  • Previous by thread: Re: Types in Mathematica
  • Next by thread: Re: Types in Mathematica