MathGroup Archive 2010

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

Search the Archive

Re: Default and head in function argument

  • To: mathgroup at smc.vnet.net
  • Subject: [mg111624] Re: Default and head in function argument
  • From: Simon <simonjtyler at gmail.com>
  • Date: Sat, 7 Aug 2010 06:30:25 -0400 (EDT)
  • References: <i3jc5s$d0n$1@smc.vnet.net>

Just to follow up Leonid's answer;
the thing to remember here is that x_ is just shorthand for x:_ and is
not always applicable

In[1]:= x_ // FullForm
Out[1]//FullForm= Pattern[x,Blank[]]

In[2]:= x:_ // FullForm
Out[2]//FullForm= Pattern[x,Blank[]]

In[3]:= x:(_Integer?Positive):3 // FullForm
Out[3]//FullForm=
Optional[Pattern[x,PatternTest[Blank[Integer],Positive]],3]

Simon

On Aug 7, 8:22 pm, Leonid Shifrin <lsh... at gmail.com> wrote:
> Hi,
>
> Clear[f];
> f[x : (_Integer?Positive) : 3] := x^3
>
> Regards,
> Leonid
>
> On Sat, Aug 7, 2010 at 9:31 AM, NP <nomplum... at gmail.com> wrote:
> > Hi,
>
> > I know how to define a function so that it evaluates only when the
> > head of the argument is of a certain type:
>
> > Clear[f];
> > f[x_Integer?Positive] := x^3
> > f[3]
> > f[3.1]
> > f[-3]
>
> > 27
> > f[3.1]
> > f[-3]
>
> > and I also know how to set default to an argument of a function like
> > so:
>
> > Clear[f]
> > f[x_:3]
> > f[]
>
> > 27
>
> > How do I do both i.e., specify head and default value?
>
> > Thanks,
>
> > NP



  • Prev by Date: Re: Random points in triangle
  • Next by Date: Re: BinLists in Array
  • Previous by thread: Re: Default and head in function argument
  • Next by thread: Re: Default and head in function argument