# How to add Optional Sequence

Note: This question could be a duplicate but I couldn't find any so far.

It is known that Optional (:) can be use to represent expression if omitted.

f[x_, y_:0]:= {x, y};
f[a]
(*{a,0}*)


Now what should I do so that I can use a sequence of values in the Optional, something like this:

f[x_, y_(if omitted gives sequence of 1,2,3)]:= {x, y};

so that If I evaluate f[a] I get {a,1,2,3}.

The method f[x_, y_: Sequence[1, 2, 3]] := {x, y} will not work because of the evaluation that happens at the time of defining f.

Of course this can be solved by giving HoldAll Attributes to the function f:

SetAttributes[f, HoldAll]
f[x_, y_: Sequence[1, 2, 3]] := {x, y}

f[a]

(*{a, 1, 2, 3}*)


But this needs to set the attributes which some times is not desired.

Question:

Is there a way to add sequence as Optional without the need to use Attributes?

Thank you

• What do you want to get for f[1,3,4]?
– Kuba
Jan 19 '16 at 9:31
• @Kuba, Perhaps I should have used f[x_,y__:options] instead of f[x_,y_:options]. Basically I am designing a graphical function which takes default styles if nothing inputted. After all, I think this approach is not the best way to do that because if I have more than one default style and I need to change only one I need then to input all of the default including the new one. May be I need to use OptionsPattern and OptionValue Jan 19 '16 at 15:23
• I see, you can put styles in a Directive.
– Kuba
Jan 19 '16 at 15:29
• Yes I could but again if I want to change only one style then I need to input all others in Directive otherwise the inputted style will be the only one will pass to RHS. For example, f[x_, y_: Directive[style1, style2]] if I want to change style2 I need to do it like this f[ firstinput, Directive[style1, newstyle]] Jan 19 '16 at 15:32

Default is another way to specify optional arguments, which allows for this:

Default[f, 2] = Sequence[1, 2, 3]
f[x_, y_.] := {x, y}
f[1, 2]


{1, 2}

f


{1, 1, 2, 3}

f[x_, y_] := {x, y}
f[x_] := {x, Sequence[1, 2, 3]}

• Probably f[x_] := {x,1,2,3} would be easier :) Jan 19 '16 at 0:39

Apart from C.E great answer, the specific answer to this question is something like this:

ClearAll[f];
f[x_, y_: Hold[1, 2, 3]] := {x, ReleaseHold@ y}
f[a, 2 + 3]
(*{w,5}*)
f[a]
(*{a, 1, 2, 3}*)


I think the canonical way to solve evaluation issues inside of a function definition is a judicious use of HoldPattern:

f[x_, HoldPattern[y_:Sequence[1, 2, 3]]] := {x, y}

f[x]


{x, 1, 2, 3}