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


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

Re: A variable defaulting to a previous variable?

Started byLeonid Shifrin <lshifr@gmail.com>
First post2011-10-07 09:02 +0000
Last post2011-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.


Contents

  Re: A variable defaulting to a previous variable? Leonid Shifrin <lshifr@gmail.com> - 2011-10-07 09:02 +0000

#5494 — Re: A variable defaulting to a previous variable?

FromLeonid Shifrin <lshifr@gmail.com>
Date2011-10-07 09:02 +0000
SubjectRe: 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?
>

[toc] | [standalone]


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


csiph-web