Re: Function parameters, using : and ?
- To: mathgroup at smc.vnet.net
- Subject: [mg2836] Re: [mg2813] Function parameters, using : and ?
- From: brucec (Bruce Carpenter)
- Date: Sat, 23 Dec 1995 03:19:16 -0500
>Hi! Is there a way of using the default pattern(:) and the match(?)
>pattern for the same argument in a function? I'm interested in writting
>something like this
>
> Func[x_List, y_List, epsilon_(?NumberQ):10^-6] := Module[{},...]
>
>but I can't get it to work. What is the problem? I must have tried all
>the different combinations of the charaters _?: without success. Any
>suggestions?
>
>- Ricardo Marimon
> rmarimon at leland.stanford.edu
I've run into this before too. The way I handle it is with the pattern /;
condition rather than pattern ? test.
In[1]:=
Func[x_List, y_List, epsilon_:10^-6] :=
Module[{},{x, y, epsilon}] /; NumberQ[epsilon]
In[2]:=
Func[{1,2,3},{a,b,c}]
Out[2]=
1
{{1, 2, 3}, {a, b, c}, -------}
1000000
In[3]:=
Func[{1,2,3},{a,b,c}, 33]
Out[3]=
{{1, 2, 3}, {a, b, c}, 33}
In[4]:=
Func[{1,2,3},{a,b,c}, d]
Out[4]=
Func[{1, 2, 3}, {a, b, c}, d]
Note that you can't put the condition inside with the default value either.
Cheers,
Bruce Carpenter