Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #5453 > unrolled thread

A variable defaulting to a previous variable?

Started byBrentt <brenttnewman@gmail.com>
First post2011-10-06 08:41 +0000
Last post2011-10-07 09:09 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  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

#5453 — A variable defaulting to a previous variable?

FromBrentt <brenttnewman@gmail.com>
Date2011-10-06 08:41 +0000
SubjectA variable defaulting to a previous variable?
Message-ID<j6jpji$mfe$1@smc.vnet.net>
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?

[toc] | [next] | [standalone]


#5502

FromSimon <simonjtyler@gmail.com>
Date2011-10-07 09:06 +0000
Message-ID<j6mff2$7l4$1@smc.vnet.net>
In reply to#5453
Maybe the easiest way would be to define 

f[x_, y_] := x + y
f[x_] := 2 x

Or maybe something like

Module[{yy}, f[x_, y_: yy] := x + y /. yy -> x]

[toc] | [prev] | [next] | [standalone]


#5508

FromPeter Pein <petsie@dordos.net>
Date2011-10-07 09:09 +0000
Message-ID<j6mfkq$7ml$1@smc.vnet.net>
In reply to#5453
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web