Hi Artin,
Let's make sure we understand what the pattern {x,y_,z} represents. It stands for a list with at least two elements (x and z) and any sequence of zero or more Wolfram Language expressions in between. It will match each of the following expressions:
{begin,end}
{begin, middle, end}
{begin, middle1, middle2, end}
In the first case, y___
matches nothing between begin
and end
. In the second case y___
matches middle
between begin
and end
. In the third case y___
matches the sequence of the two expressions middle1
-middle2
between begin
and end
.
In[1]:= MatchQ[#, {x_, y___, z_}] & /@ {{begin, end}, {begin, middle,
end}, {begin, middle1, middle2, end}}
Out[1]= {True, True, True}
If ___
matches a sequence of length more than one, then the sequence will be represented by a Sequence object. Sequence automatically splices its argument into any function with which it is used. So you get a Sequence for y___
:
In[2]:= {begin, middle1, middle2, end} /. {x_, y___, z_} :> y
Out[2]= Sequence[middle1, middle2]
When you replace y
with the sequence in the Power expression Power[y,z]
with:
Power[y, z] /. y -> Sequence[middle1, middle2]
what you end up with is:
In[4]:= Power[middle1, middle2, z]
Out[4]= middle1^middle2^z
because Power[x,y,z. ...] is taken to be Power[x,Power[y,z,...]]
Now back to your question, how can we get {two^four, three^four}
? The following would be one way to do it:
In[17]:= SequenceCases[{one, two, three, four}, {x_, y_, z_} :> y^z, Overlaps -> True]
Out[17]= {two^three, three^four}
Remember arguments to a function are evaluated before they are passed to the function unless you set its attribute using something like Hold*.
Head[Sequence[two,three]]
is really seen as
Head[two,three]
and we know from the documentation Head[expr,h] wraps the result with h.
Head of two
is Symbol
which is therefore wrapped with three
.
You can see the Head of a Sequence by holding it unevaluated.
In[27]:= Head[Unevaluated[Sequence[two, three]]]
Out[27]= Sequence