Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #5508
| From | Peter Pein <petsie@dordos.net> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: A variable defaulting to a previous variable? |
| Date | 2011-10-07 09:09 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <j6mfkq$7ml$1@smc.vnet.net> (permalink) |
| 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
Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Find similar | Unroll thread
A variable defaulting to a previous variable? Brentt <brenttnewman@gmail.com> - 2011-10-06 08:41 +0000 Re: A variable defaulting to a previous variable? Simon <simonjtyler@gmail.com> - 2011-10-07 09:06 +0000 Re: A variable defaulting to a previous variable? Peter Pein <petsie@dordos.net> - 2011-10-07 09:09 +0000
csiph-web