MathGroup Archive 2012

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

Search the Archive

Re: set option flag in function definition

  • To: mathgroup at smc.vnet.net
  • Subject: [mg128160] Re: set option flag in function definition
  • From: Murray Eisenberg <murray at math.umass.edu>
  • Date: Wed, 19 Sep 2012 05:00:19 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net
  • References: <20120918074139.41A2D6853@smc.vnet.net>

On Sep 18, 2012, at 3:41 AM, Joug Raw <jougraw at gmail.com> wrote:

> ...I want to have a function that executes certain operation when I chose the
> corresponding option flag.
>
> For example,
>
> a function called "f"
>
> when I call it like f[arg1,arg2,arg3, optionX]
> it executes command
> arg1/arg2+arg3
>
> If I call it like f[arg1,arg2,arg3,optionY]
> it executes command
> arg1*arg2-arg3
>
> If I call it like f[arg1,arg2,arg3,optionSomethingElse]
> it returns me
> "No appropriate option flag was found. Please indicate correct options"
>
> ...I am wondering if I can do
> the same in Mathematica easily?


Yes, it can be done very easily in Mathematica:

  f[a_, b_, c_, method -> "one"] := a/b+c
  f[a_, b_, c_, method -> "two"] := a b-c
  f[a_, b_, c_, method -> _] := "No relevant value for 'method' found"
  f[a_, b_, c_, opt_ -> _] := "I don't understand this option"

(I shortened argument names; the option name "method" I used is quite arbitrary.)

The key to the above is that Mathematica always uses a more specific rule before it uses a more general one.

If, though, you want a single expression for defining the function f, then yes that's also possible but more complicated; you cold use, e.g., FilterRules.

---
Murray Eisenberg                                     
murray at math.umass.edu
Mathematics & Statistics Dept.      
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                  413 545-2859 (W)
710 North Pleasant Street                      fax   413 545-1801
Amherst, MA 01003-9305








  • Prev by Date: Re: Linear combinations of Expectation of EmpiricalDistribution
  • Next by Date: Re: set option flag in function definition
  • Previous by thread: set option flag in function definition
  • Next by thread: Re: set option flag in function definition