Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #15876 > unrolled thread
| Started by | Oliver Ruebenkoenig <ruebenko@wolfram.com> |
|---|---|
| First post | 2011-02-04 06:37 +0000 |
| Last post | 2011-02-04 06:37 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.soft-sys.math.mathematica
Re: Mixing optional parameters and options Oliver Ruebenkoenig <ruebenko@wolfram.com> - 2011-02-04 06:37 +0000
| From | Oliver Ruebenkoenig <ruebenko@wolfram.com> |
|---|---|
| Date | 2011-02-04 06:37 +0000 |
| Subject | Re: Mixing optional parameters and options |
| Message-ID | <iig6s5$rdb$1@smc.vnet.net> |
Peter,
On Thu, 3 Feb 2011, Peter Breitfeld wrote:
>
> To give a simpe example which, I hope, makes clear my issue:
> Suppose I define this (silly) example function:
>
>
> Options[f]={bla->True};
> f[x_,y_:11,OptionsPattern[]:=If[OptionValue[bla],x*y,y/x]
>
> Now:
> f[5] ----> 55 (ok)
> f[5,7] ----> 35 (ok)
>
> f[5,7,bla->False] ----> 7/5 (ok)
> f[5,bla->False] ----> 5*(bla->False)
>
> I expected the result 11/5
>
> The only way I found was to define f like this:
>
> f[{x_,y_:11},OptionsPattern[]]
>
> Can it be done without the curly braces?
>
> Thank you
>
you could use this
Options[f] = {bla -> True};
f[x_, y_: 11, OptionsPattern[]] /; NumericQ[y] :=
If[OptionValue[bla], x*y, y/x]
hth,
Oliver
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web