Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

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

Search the Archive

Re: Defining a Function with an Indeterminate Number of Arguments

  • To: mathgroup at smc.vnet.net
  • Subject: [mg81269] Re: Defining a Function with an Indeterminate Number of Arguments
  • From: Szabolcs Horvát <szhorvat at gmail.com>
  • Date: Tue, 18 Sep 2007 00:38:19 -0400 (EDT)
  • References: <fclbai$fh8$1@smc.vnet.net>

Donald DuBois wrote:
> Hello -
> 
> I am trying to define a function with the following two properties :
>   
>   (A) The function should be able to take an indeterminate number of 
> arguments without using the List structure (like the Which or StringJoin functions in Mathematica).
>   
>   (B) The function should have the capability of defining and using options in the usual way  (i.e. using the Options function outside the package definition
> to define the option default values and using OptionValue within the function definition to retrieve the options with locally defined option values overriding the default values). 
> 

How about this (the example from OptionValue's doc page, adapted to do 
what your function does):

Options[fun] = { opt1 -> 10, opt2 -> 20 }

fun[arg___, OptionsPattern[]] := {{arg}, Head/@{arg}, OptionValue[opt1], 
OptionValue[opt2]}

?

> (* Define Default Options *)
> Options[fnc2] =
>     {
>       opt1 -> 10,
>       opt2 -> 20
>       };
> 
> fnc2[args : _ ..] := Module[{},
>       
>       (* Extract the non-rule arguments from args *)
>       
>       values = Cases[{args}, Except[_Rule]];
>    
>       Print["Argument Values: "];
>       Table[
>     Print[i, "   ", values[[i]], " Head ", Head[values[[i]]]], {i, 
>      Length[values]}];
>       
>       (* Extract the options from args *)
>       localOpts = Cases[{args}, _Rule];
>    
>       Print["local options list = ", localOpts];
>    
>    Print["Final Option Values: "];
>       Print["opt1 = ", 
>         localOpts /. OptionsPattern[] :> OptionValue[opt1]];
>       Print["opt2 = ", 
>         localOpts /. OptionsPattern[] :> OptionValue[opt2]];
>       
>       ] (* End Module *);
> 
> fnc2[b, 3, "strg1", opt1 -> 15]

You did not use OptionsPattern[] in the usual way here, so it does not 
know where to take the default options from.  Change it to 
OptionsPattern[fnc2], and it will work.  But I still recommend the 
alternate solution I presented above.  Don't use Print[], Module[], ;, 
and other ugly procedural constructs when you don't need them.

-- 
Szabolcs


  • Prev by Date: Re: Defining a Function with an Indeterminate Number of Arguments
  • Next by Date: Re: LegendreP error (bug?) in Mathematica
  • Previous by thread: Re: Defining a Function with an Indeterminate Number of Arguments
  • Next by thread: SWF runs to fast