Mathematica 9 is now available
Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Mathematica language issues

  • To: mathgroup at
  • Subject: [mg52955] Mathematica language issues
  • From: ab_def at (Maxim)
  • Date: Fri, 17 Dec 2004 05:20:39 -0500 (EST)
  • Sender: owner-wri-mathgroup at

All the typical issues with the Mathematica programming language are
still present in version 5.1:

  Module[{x = 0},
    While[x++; EvenQ[x] ];

still evaluates to 3, and it is easy to construct other similar

  {L = Partition[Range[8], 2]},
  Table[L[[i, j]], {i, 4}, {j, If[EvenQ[i], 1, 2]}]

    {L = Partition[Range[8], 2]},
    Table[L[[i, j]], {i, 4}, {j, If[EvenQ[i], 1, 2]}]

{{1, 2}, {3}, {5, 6}, {7}}

{{1, 2}, {3, 4}, {5, 6}, {7, 8}}

In the second case EvenQ[i] is evaluated before i is assigned a
numerical value, and therefore for each i the inner iterator is {j,
2}. Thus pretty much all that is known about Compile is that the
result will work roughly similar to the uncompiled code. Nothing
beyond that is guaranteed.

The issues with Unevaluated are all here as well, from really serious
problems such as

Unevaluated[D[y, x]] /. {y -> x}
Unevaluated[D[y, x]] /. {{y -> x}}



where you can find out how many Unevaluated wrappers are required in
each case only by trial and error, to curious glitches like

Unevaluated[1 + 1]*2
2*Unevaluated[1 + 1]


2*Unevaluated[1 + 1]

Next, there's the question of how various functions act on patterns.
Suppose you want to convert Max[x, -Infinity] to x and write

Max[x, -Infinity] /. Max[x_, -Infinity] -> x

This will not work, because Max[x, -Infinity] is left unevaluated, but
Max[x_, -Infinity] evaluates to x_. Besides, when _+_ evaluates to 2_,
this is not only counter-intuitive, but also not very logical, because
unnamed patterns are supposed to stand for two possibly different

Another example:

x /: x + (a_.) = a; 


If, provided that we define f[y+a_.]=a, f[y] evaluates to 0, I can't
see why it should work differently in the above case.

Then there are the old problems with pattern matching:

a + b + c /. Plus[a, b] :> 0
a + b + c /. s : Plus[a, b] :> 0
a + b + c /. Plus[a, b] /; True :> 0
a + b + c /. HoldPattern[Plus][a, b] :> 0

The first expression evaluates to c, others leave a + b + c unchanged.
Adding a name for the pattern, putting a condition on the left-hand
side of the pattern or wrapping the head of the expression in
HoldPattern all break pattern matching for Plus. More precisely, in
those cases Mathematica will still recombine the arguments of Plus to
transform a + b + c into Plus[Plus[a, b], c] and so on, but the
replacement rules will work only on the expression as a whole, not on
subexpressions such as Plus[a, b].

Here is a variation along the same lines, based on Paul Buettiker's
post (

a + b /. HoldPattern[Plus[s__]] :> s
a + b /. s2 : HoldPattern[Plus[s__]] :> s

a + b

Sequence[a, b]

The only difference is that in the second example we add a name for
the pattern.

Maxim Rytin
m.r at

  • Prev by Date: Re: Minors
  • Next by Date: Re: Many keys not working under Linux Mandrake 10.1
  • Previous by thread: NonlinearRegress+NIntegrate
  • Next by thread: Re: Mathematica language issues