MathGroup Archive 2009

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

Search the Archive

RE: Passing function arguments as lists of replacement rules

Use the newer Version 6 Options facilities.

Options[foo] = {a -> 0, b -> 0, c -> 0};
SyntaxInformation[foo] = {"ArgumentsPattern" -> {OptionsPattern[]}};
foo[OptionsPattern[]] :=
  {aval = OptionValue[a],
   bval = OptionValue[b],
   cval = OptionValue[c]},
  {aval, bval, cval}]

foo[a -> 1, b -> 2]
{1, 2, 0}

Then try typing:

foo[a -> 1, q -> 2]

David Park
djmpark at  

From: dnquark [mailto:dnquark at] 

I wish to (a) avoid having to pass a dozen parameters to a function
and have to deal with remembering which goes where and (b) retain the
flexibility in terms of adding new parameters.  It seems that a good
solution would be to pass my parameters as a structure with named
fields (this is how I do it in another system).  In Mathematica, I came up
with something like this:

foo[paramList_] := Module[{a,b,c},
{a,b,c} = {"a","b","c"}/.paramList;
sample usage: e.g.  foo[{"a"->1,"b"->2}]

My question to the group: is this a good solution?  Are there better
ways to achieve my goals?..

  • Prev by Date: Re: Monitoring using Mathematica
  • Next by Date: Re: Mysterious Output in formatted Notebook
  • Previous by thread: Re: Passing function arguments as lists of replacement rules
  • Next by thread: Mathematica skill level snippet(s)