How do I test if a variable represents a sequence?

You may make a helper function with attribute SequenceHold.

ClearAll[sequenceQ];
Attributes[sequenceQ] = {SequenceHold};
sequenceQ[_Sequence] = True;
sequenceQ[_] = False;

This can be used to test for a sequence directly or with mapping and threading functions as can the built in *Q functions. Note that I use Set here instead of SetDelayed as it works better for this particular function definition.

mySeq = Sequence[1, 2, 3];
sequenceQ[mySeq]

(* True *)

Hope this helps.


This tests if a symbol represents a Sequence:

With[{a = mySeq}, HoldComplete[a][[1, 0]]] === Sequence

or:

(mySeq -> 0)[[1, 0]] === Sequence

Internal`InheritedBlock[{FreeQ},
 SetAttributes[FreeQ, SequenceHold];
 FreeQ[mySeq, 1]
]
False

Tags:

Sequence