MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: A variable defaulting to a previous variable?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg121963] Re: A variable defaulting to a previous variable?
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Fri, 7 Oct 2011 04:49:02 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201110060825.EAA22765@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 at 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?
>


  • Prev by Date: Re: list equation
  • Next by Date: Re: list equation
  • Previous by thread: Re: A variable defaulting to a previous variable?
  • Next by thread: Re: A variable defaulting to a previous variable?