Let's consider a function
f[a_,b___]
I want that if I input only one argument I get
f[a]=a
but if I input 2 arguments I get
f[a,b]=2a+b
How can I implement that?
I thought I could use If:
f[a_,b___]:=If[b===Null,a,2a+b]
but reading this previous post Are UnsamQ and Not[SameQ] the same function? I think it's not the correct way.
I could use 2 different definition for f[a_] and f[a_,b_] but I'd like to have only one definition. In fact, my problem arises because if f has only one argument, f performs analytic calculation, but with two argument (where the second argument is a "correction" of the first one) f needs, in only one part, a numerical calculations; anyway the computation that f performs is the same in both cases.
f[a_] := a; f[a_, b__] := 2a+b. In your code, you could useMatchQ[Hold[b], Hold[]]for the condition test. (You could also useSameQinstead ofMatchQ.) – Michael E2 May 31 '17 at 10:31f[x__] := If[Length[{x}] == 1, First[{x}], 2 First[{x}] + Last[{x}]]. – Kiro May 31 '17 at 10:38