Re: Sequence is funny !

• To: mathgroup at smc.vnet.net
• Subject: [mg8904] Re: [mg8852] Sequence is funny !
• From: Olivier Gerard <jacquesg at pratique.fr>
• Date: Thu, 2 Oct 1997 22:56:57 -0400
• Sender: owner-wri-mathgroup at wolfram.com

```At 02:16 +0200 97.10.01, Gilles BARBIER wrote:
>      For some people who like to understand the internal mechanism
>      of Mathematica, I give them this little problem a friend sent to me :
>
>      Why Table[i,{i,Sequence[1,3]}] gives {1,2,3,4} instead of {1,2,3} !!
>
>      That's not a bug, I even have an explanation,if you are interested.
>
>      Hope it's fun,
>
>      Gilles.

Gilles, Indeed it is fun !
Here is my explanation:

- When Mma evaluates the Iterator in the table, the Sequence
is not already expanded and it uses a pattern of the kind
{i_Symbol, upperlimit_} .
As the use of Trace and the option TraceInternal shows,
in this case, the upperlimit is being substracted then added 1
then 'floored' (certainly to cause evaluation and test various conditions
related to the fact that the starting case will be 1 and that
Mma must be sure that it can be incremented and decremented,
(in this case the increment is always 1)).

At the first operation on the sequence object (substracting 1)
the two elements of the Sequence Object are spliced into the Plus
clause. As this Sequence object comports a 1 along with the 3,
the result is  Plus[1, 3, -1] = 3.
The correcting +1 hereafter gives +4, and now you have an element
more in the list...

As a demonstration of more details, I recommend this example:

In[1]:=Table[ i, {i,Sequence[1,-2,5],10}]

Out[1]:=
{1,-2,5,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}

Funny, isn't it ?

This simpler one shows that you can probably do a Map with Table
as long as your values are numerical:

In[2]:=Table[ i, {i,Sequence[1,-5,-2],10}]

Out[2]=
{1,-5,-2}

Sequence is a very powerful instruction whose typical
use is to transgress the classical expression bareers.
This is no surprise it can be used to radically change
evaluation.

Olivier Gerard

Editor in Chief - Special Issues
French Mathematics Quarterly

PS:
Of course, all that is not tied to Table but to the iterator
syntax. But there is something amusing I have found trying
this with Do:

In[3] := Do[ Print[i], {i,1,3}]

1
2
3

but if you try

In[4] := Do[ Print[i], {i,Sequence[1,-5,-2],10}]

1-5-2

(without newlines between them)

```

• Prev by Date: RE: := vs. =
• Next by Date: Re: Pasting equations into word etc
• Previous by thread: Re: Sequence is funny !
• Next by thread: Re: Sequence is funny !