From: NP on
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

From: Leonid Shifrin on
Hi,

Clear[f];
f[x : (_Integer?Positive) : 3] := x^3


Regards,
Leonid


On Sat, Aug 7, 2010 at 9:31 AM, NP <nomplume69(a)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
>
>
From: Simon on
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...(a)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...(a)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


From: David Park on
f[x : (_Integer?Positive) : 3] := x^3

{f[3], f[], f[3.1], f[-3]}

{27, 27, f[3.1], f[-3]}

The parentheses in the definition are necessary.

Here is a somewhat more complicated case with two default arguments. The
first one has alternatives. As long as Mathematica can distinguish the
conditions you can omit either of the two arguments, or both of them.

g[x : (_Integer?Positive | Sqrt[2]) : 3,
y : (_Integer?(# <= 0 &)) : 0] := {x, y}

{g[], g[4], g[Sqrt[2]], g[-2], g[.5, -2], g[3, -1/2], g[-1, 3]}

{{3, 0}, {4, 0}, {Sqrt[2], 0}, {3, -2}, g[0.5, -2], g[3, -(1/2)],
g[-1, 3]}


David Park
djmpark(a)comcast.net
http://home.comcast.net/~djmpark/


From: NP [mailto:nomplume69(a)gmail.com]


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



From: Scot T. Martin on
I believe that you're after:

f[x:_Integer?(Positive):3] := x^3

The important point is the use of "(...)" to keep Mathematica oriented
about your intentions.


On Sat, 7 Aug 2010, NP 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
>
>