Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #5494 > unrolled thread
| Started by | Leonid Shifrin <lshifr@gmail.com> |
|---|---|
| First post | 2011-10-07 09:02 +0000 |
| Last post | 2011-10-07 09:02 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.soft-sys.math.mathematica
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: A variable defaulting to a previous variable? Leonid Shifrin <lshifr@gmail.com> - 2011-10-07 09:02 +0000
| From | Leonid Shifrin <lshifr@gmail.com> |
|---|---|
| Date | 2011-10-07 09:02 +0000 |
| Subject | Re: A variable defaulting to a previous variable? |
| Message-ID | <j6mf7d$7ip$1@smc.vnet.net> |
Here is one way:
ClearAll[f];
Module[{z},
f[x_, y_: z] := Block[{z = x}, x + y];
]
For more details and an explanation, see
http://stackoverflow.com/questions/7394113/defining-a-function-with-an-optional-value-that-is-by-default-a-function-of-anoth/7394668#7394668
Regards,
Leonid
On Thu, Oct 6, 2011 at 12:25 PM, Brentt <brenttnewman@gmail.com> wrote:
> 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?
>
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web