9
$\begingroup$

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

$\endgroup$
4
  • $\begingroup$ What do you want to get for f[1,3,4]? $\endgroup$
    – Kuba
    Jan 19, 2016 at 9:31
  • $\begingroup$ @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 $\endgroup$ Jan 19, 2016 at 15:23
  • $\begingroup$ I see, you can put styles in a Directive. $\endgroup$
    – Kuba
    Jan 19, 2016 at 15:29
  • $\begingroup$ 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]] $\endgroup$ Jan 19, 2016 at 15:32

4 Answers 4

9
$\begingroup$

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, 1, 2, 3}

$\endgroup$
2
$\begingroup$
f[x_, y_] := {x, y}
f[x_] := {x, Sequence[1, 2, 3]}
$\endgroup$
1
  • $\begingroup$ Probably f[x_] := {x,1,2,3} would be easier :) $\endgroup$ Jan 19, 2016 at 0:39
2
$\begingroup$

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}*)
$\endgroup$
2
$\begingroup$

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}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.