Re: Functions with variable number of arguments and
- To: mathgroup at smc.vnet.net
- Subject: [mg105903] Re: [mg105884] Functions with variable number of arguments and
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Thu, 24 Dec 2009 00:16:20 -0500 (EST)
- References: <200912230741.CAA21793@smc.vnet.net>
Hi Thomas, One way would be to make a stricter type-checks, making sure that "normal" optional arguments don't match the option pattern. Here I define a predicate checking that the argument is not an option or list of options (based on OptionQ - should be easy to use OptionPattern instead if needed): In[1]:= Clear[notOptionQ]; notOptionQ[x_] := ! OptionQ[x]; In[3]:= Clear[ff]; Options[ff] = {opt -> "DefaultOptionValue"}; SyntaxInformation[ ff] = {"ArgumentsPattern" -> {{_, _., _.}, OptionsPattern[]}}; ff[x_, y_?notOptionQ, z_?notOptionQ, OptionsPattern[]] := {"ff", x, y, z, OptionValue[opt]} ff[x_, y_?notOptionQ, OptionsPattern[]] := {"ff", x, y, 0, OptionValue[opt]} ff[x_, OptionsPattern[]] := {"ff", x, 0, 0, OptionValue[opt]} In[9]:= ff[1, opt -> "SpecialValue"] ff[2, 2, opt -> "SpecialValue"] ff[3, 3, 3, opt -> "SpecialValue"] Out[9]= {"ff", 1, 0, 0, "SpecialValue"} Out[10]= {"ff", 2, 2, 0, "SpecialValue"} Out[11]= {"ff", 3, 3, 3, "SpecialValue"} In[12]:= Clear[gg]; Options[gg] = {opt -> "DefaultOptionValue"}; SyntaxInformation[ gg] = {"ArgumentsPattern" -> {{_, _., _.}, OptionsPattern[]}}; gg[x_, y : (_?notOptionQ) : 0, z : (_?notOptionQ) : 0, OptionsPattern[]] := {"g", x, y, z, OptionValue[opt]} (* Note that the parentheses around _?notOptionQ pattern are essential here *) In[16]:= gg[1, opt -> "SpecialValue"] gg[2, 2, opt -> "SpecialValue"] gg[3, 3, 3, opt -> "SpecialValue"] Out[16]= {"g", 1, 0, 0, "SpecialValue"} Out[17]= {"g", 2, 2, 0, "SpecialValue"} Out[18]= {"g", 3, 3, 3, "SpecialValue"} Hope this helps. Regards, Leonid On Wed, Dec 23, 2009 at 10:41 AM, Thomas M=FCnch <thomas.muench at gmail.com>w= rote: > Dear Mathgroup, > > I can't figure out how to define a function properly that has a variable > number of arguments and options. When I call the function with less than > the full set of arguments, I want the missing arguments to take on > default values. Below are two toy examples, functions f and g, that both > behave identical. How do I make the options work when I call the > function with less than the full set of arguments? > > Thank you for your help, > > thomas > > > Clear[f]; > Options[f]={opt->"DefaultOptionValue"}; > SyntaxInformation[f]={"ArgumentsPattern"->{{_,_.,_.},OptionsPattern[]}}= ; > f[x_,y_,z_,OptionsPattern[]]:={"f",x,y,z,OptionValue[opt]} > f[x_,y_,OptionsPattern[]]:={"f",x,y,0,OptionValue[opt]} > f[x_,OptionsPattern[]]:={"f",x,0,0,OptionValue[opt]} > > Clear[g]; > Options[g]={opt->"DefaultOptionValue"}; > SyntaxInformation[g]={"ArgumentsPattern"->{{_,_.,_.},OptionsPattern[]}}= ; > g[x_,y_:0,z_:0,OptionsPattern[]]:={"g",x,y,z,OptionValue[opt]} > > (* The following simple function calls work as expected, both for f and > for g *) > > f[1] > f[2,2] > f[3,3,3] > > g[1] > g[2,2] > g[3,3,3] > > (* But if one wants to specify a different value for the option, it only > works if the full number of arguments are supplied, in this case 3 > arguments. Otherwise, the option is interpreted as the second or third > argument instead of an option: *) > > f[1,opt->"SpecialValue"] > f[2,2,opt->"SpecialValue"] > f[3,3,3,opt->"SpecialValue"] (*only this works*) > > g[1,opt->"SpecialValue"] > g[2,2,opt->"SpecialValue"] > g[3,3,3,opt->"SpecialValue"] (*only this works*) > >
- References:
- Functions with variable number of arguments and options
- From: Thomas Münch <thomas.muench@gmail.com>
- Functions with variable number of arguments and options