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


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

Re: A variable defaulting to a previous variable?

From Leonid Shifrin <lshifr@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: A variable defaulting to a previous variable?
Date 2011-10-07 09:02 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <j6mf7d$7ip$1@smc.vnet.net> (permalink)
References <201110060825.EAA22765@smc.vnet.net>

Show all headers | View raw


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 comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web