Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: A variable defaulting to a previous variable?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg121977] Re: A variable defaulting to a previous variable?
  • From: Peter Pein <petsie at dordos.net>
  • Date: Fri, 7 Oct 2011 04:51:34 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <j6jpji$mfe$1@smc.vnet.net>

Am 06.10.2011 10:41, schrieb Brentt:
> How would one go about making a variable default to a previous variable?
> 
> I would like results like this:
> 
> In[1]= f[x_, y_: x] := x+y;
>> In[2]= f[2,3]
>> Out[1]= 5
>> In[3]= f[3]
>> Out[1]= 6
>>
> 
> 
> 
> But, of course, Mathematica won't let me use a previous argument for the
> default of the next argument. Is there a way to do this?

In[1]:= f[x_, y___] := If[{y} === {}, 2*x, x + y]
In[2]:= f[2, 3]
Out[2]= 5
In[3]:= f[3]
Out[3]= 6




  • Prev by Date: FFT Speed
  • Next by Date: Re: list equation
  • Previous by thread: Re: A variable defaulting to a previous variable?
  • Next by thread: How to simplify a finite sum plus the next element?