10
$\begingroup$

As we know,we can define a function with default-value like this:

fun1[x_, n_: 1, a_: 2, b_: 3] := x + n + a + b

In[6]:= fun1[4]

Out[6]= 10

But in my case I hope the a and b in a list,So I define it like:

fun2[x_, n_: 1, {a_: 2, b_: 3}] := x + n + a + b

In[8]:= fun2[5]

Out[8]= fun2[5]

It's failure.And I have a try this,it is failue too:

fun3[x_, n_: 1, l_List: {a_: 2, b_: 3}] := x + n + a + b

It have a strange result:

In[16]:= fun3[5]

Out[16]= 6 + a + b

$\endgroup$
0

4 Answers 4

9
$\begingroup$

EDIT 03 / 02 / 2018

Please note that starting with version 11.2, the behavior of the pattern-matcher was changed so that the solution below no longer works. The problem with the solution below is that it makes it possible for a variable pointing at entire pattern, to get bound to something else than the expression where inner pattern variables get substituted with their default values. That is inconsistent, and that is one reason why the behavior of the pattern matcher has been modified.

--------------------------

The problem here is that the pattern you used, requires a List as a last argument in all cases. A possible workaround will be, for example:

ClearAll[fun2];
fun2[x_, n_: 1, p : {a_: 2, b_: 3} : {}] := x + n + a + b

So that

fun2[5]
fun2[5, 2, {1}]
fun2[5, 10, {1, 2}]

(*
  11

  11

  18
*)
$\endgroup$
4
  • $\begingroup$ +Infinity.Very interesting. $\endgroup$
    – yode
    Mar 5, 2016 at 18:35
  • $\begingroup$ When I run this function in my Mathematica 11.2, fun2[5] gives 6. But when I run it in Mathematica 11.1.1 in another computer, fun2[5] gives 11. What is wrong? $\endgroup$
    – renphysics
    Mar 1, 2018 at 14:46
  • $\begingroup$ @renphysics I have discussed this with colleagues. Apparently, the current behavior (returning 6) is correct, while the one I used above, while convenient, is not. The main point is this: if p becomes an empty list by default, that should mean that both a and b become Sequence[] ("nothing"), and then when used in Plus, they don't contribute. The change has indeed been introduced after 11.1 $\endgroup$ Mar 1, 2018 at 20:44
  • $\begingroup$ @renphysics What the pattern language really lacks here is the primitive like AutomaticDefault, so that we could have fun2[x_, n_: 1, p : {a_: 2, b_: 3} : AutomaticDefault] := x + n + a + b, which would mean that the value of p is determined from the values of inner constituents (bound pattern variables). In this case, it would result in p being bound to {2, 3} and fun2[5] returning 11, as we want it here. $\endgroup$ Mar 1, 2018 at 20:45
4
$\begingroup$

Give a default value to the list instead:

fun2[x_, n_: 1, a_List: {2, 3}] := x + n + Plus@@a
fun2[10]
(* 16 *)
$\endgroup$
4
  • $\begingroup$ Thanks your answer.you almost solve my problem.But there a blemish still that the 2 and 3 have not its name.Of course we can use a[[1]] or a[[2]] to extract its value.Could you help me name the default-value 2 as a and the 3 as b in the fun2? $\endgroup$
    – yode
    Mar 5, 2016 at 18:20
  • $\begingroup$ Fool me long time.And look forward to your response.As your answer ,your fun2 work well.Like fun2[1, 4, {7, 8}] will give a right result.But when I built a new function.fun[a_, l_list: {2, 3}] := a + Plus @@ l.The fun[1, {4, 5}] will give up to compute. $\endgroup$
    – yode
    Mar 7, 2016 at 17:57
  • $\begingroup$ @yode. You need to capitalize list in the function definition: fun[a_, l_List: {2, 3}] := a + Plus @@ l works. $\endgroup$
    – march
    Mar 7, 2016 at 18:17
  • $\begingroup$ God~The typo harm me deeply. $\endgroup$
    – yode
    Mar 8, 2016 at 20:25
3
$\begingroup$

Other method is to add two definitions as follows:

ClearAll[fun2];
fun2[x_, n_: 1, {a_: 2, b_: 3}] := x + n + a + b
fun2[x_, n_: 1, a_: 2, b_: 3] := x + n + a + b

fun2[5]
fun2[5, 2]
fun2[5, 2, {1}]
fun2[5, 10, {1, 2}]
(*
  11
  12
  11
  18
*)
$\endgroup$
2
  • $\begingroup$ Could you add some explanation what about this effect??? $\endgroup$
    – yode
    Mar 5, 2016 at 22:46
  • $\begingroup$ @yode, This is a pattern matching where fun2[5] and fun2[5, 2] match the definition fun2[x_, n_: 1, a_: 2, b_: 3] and the evaluation happens. fun2[5, 2, {1}] and fun2[5, 10, {1, 2}] match fun2[x_, n_: 1, {a_: 2, b_: 3}] and the evaluation happens also. $\endgroup$ Mar 6, 2016 at 4:42
3
$\begingroup$

You can also go for something as simple as function overloading:

f[x_, n_:1] := f[x, n, {2, 3}];
f[x_, n_, {a_, b_}] := x + n + a + b;
$\endgroup$

Your Answer

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

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