Re: When is a List not a List?

• To: mathgroup at smc.vnet.net
• Subject: [mg90977] Re: When is a List not a List?
• From: Albert Retey <awnl at gmx-topmail.de>
• Date: Fri, 1 Aug 2008 03:01:21 -0400 (EDT)
• References: <g6rntk\$7kj\$1@smc.vnet.net>

```Hi,
>    g[x_, n_] := x^n
>    FullForm[Table[g[x, n], {n, 1, 2}]]
>    FullForm[{g[x, 1], g[x, 2]}]
>    Plot[{g[x, 1], g[x, 2]}, {x, 0, 1}, PlotStyle -> {Red, Blue}]
>    Plot[Table[g[x, n], {n, 1, 2}], {x, 0, 1}, PlotStyle -> {Red, Blue}]
>
> The FullForm[]s are identical.

Plot has Attribute HoldAll, and:

FullForm[Hold[Table[g[x, n], {n, 1, 2}]]]
FullForm[Hold[{g[x, 1], g[x, 2]}]]

are not the same expressions. This means that Plot sees the unevaluated
expressions which are different and so there is no quirk in that it
behaves differently when it sees a list. Of course one could argue that
it could analyze the Table command and find out that that will evaluate
to a two element list, but this is in general not possible without
complete evaluation. E.g.: can you tell how many elements the following
table will have without evaluating it?:

Table[If[RandomReal[]<0.5,x^i,Unevaluated[Sequence[]]],{i,1,10}]

so in these cases it will not try to be smart. If you explicitly tell
Plot to evaluate:

Plot[Evaluate[Table[g[x, n], {n, 1, 2}]], {x, 0, 1}, PlotStyle -> {Red,
Blue}]

or in Version 6 also:

Plot[Table[g[x, n], {n, 1, 2}], {x, 0, 1}, PlotStyle -> {Red, Blue},
Evaluated -> True]

a red and blue curve are shown because now Plot handles identical
expressions (the evaluated ones that you have been looking at).

I think you would find a lot less "bugs" and "quirks" if you would try
to understand the basic mechanisms of how mathematica works. I always
following to the adress field within the documentation center):

tutorial/EvaluationOfExpressionsOverview

By reading I mean you definitly should take the time to evaluate the
examples and experiment with them to make sure you get the point.

hth,

albert

```

• Prev by Date: Re: When is a List not a List?
• Next by Date: Re: When is a List not a List?
• Previous by thread: Re: When is a List not a List?
• Next by thread: Re: When is a List not a List?