I just considered if/how one could implement Sequence in Mathematica if it were not predefined. It turned out that the following simple definition has in all my tests exactly the right behaviour:
myseq /: f_[x___, myseq[y___], z___] := f[x, y, z]
Now my question: Does this already correctly reproduce the full behaviour of Sequence, or is there something Sequence does but myseq doesn't which I missed in my tests?
Here's what I tested:
foo[myseq[a, b]]
(*
==> foo[a, b]
*)
Hold[mysec[a,b]]
(*
==> Hold[a, b]
*)
HoldComplete[myseq[a,b]]
(*
HoldComplete[myseq[a, b]]
*)
Hold[f[myseq[a,b]]]
(*
==> Hold[f[myseq[a, b]]]
*)
f[myseq[myseq[a,b],c,d],e,myseq[f,g,myseq[]]]
(*
==> f[a, b, c, d, e, f, g]
*)
myseqfails is for something like{1, 2, 3} /. 2 -> myseq[a, b]. – Heike Jun 18 '12 at 18:57t = {{a, b}, {c, d}, f}; myseq @@tfails – Dr. belisarius Jun 18 '12 at 18:58SequenceHoldlikeSequncedoes. – Szabolcs Jun 18 '12 at 18:58myseq[1,2]does the same. It may also be due to the lack ofSequenceHoldsupport. EDIT: it's not. Support can be added withmyseq /: f_[x___, myseq[y___], z___] /; FreeQ[Attributes[f], SequenceHold] := f[x, y, z]– Szabolcs Jun 18 '12 at 19:00RuleI notice an attribute with the suggestive nameSequenceHold. Adding/; !MemberQ[Attributes[f], SequenceHold]to the definition seems to fix this problem. Any others? – celtschk Jun 18 '12 at 19:07t = {...}; myseq @@tfail? I get the same result as withSequence, except that of course I have amyseqinstead ofSequencein the output. Is that what you considered "failing"? – celtschk Jun 18 '12 at 19:15myseq[1, 2]gives me an error aboutIfbeing called with 6 arguments, possibly during the formatting step (ToBoxes) – Szabolcs Jun 18 '12 at 19:16SequenceHoldtest doesn't fix it either. – celtschk Jun 18 '12 at 19:25TraceorBlockthe evaluation that happens during formatting, not evaluating the input expression. I always wanted to ask a question on this but I thought the answers wouldn't bring anything new ... maybe it's time to ask that question soon: in this caseToBoxescan't emulate the formatting step completely. – Szabolcs Jun 18 '12 at 19:32