Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Re: Optional Alternatives


A missing value can't really fit a pattern, but you can do this:

Clear[f]
f[x:_Real | _Rational] := x
f[x_Integer] := 2
f[] := f[GoldenRatio]
f /@ {0, 1/2, 0.3, Pi}
{2, 1/2, 0.3, f[Pi]}

f[]
f[GoldenRatio]

Or...

Clear[f]
Default[f] = GoldenRatio;
f[x:_Real | _Rational] := x
f[x_Integer] := 2
f[x_Symbol] := Symbol
f[x_.] := f[Floor[x]]^2
f /@ {0, 1/2, 0.3, Pi}
{2, 1/2, 0.3, Symbol}

f[]
4

In both cases, I've made GoldenRatio the default value.

Bobby

On Wed, 11 Aug 2004 05:53:08 -0400 (EDT), Selwyn Hollis <sh2.7183 at misspelled.erthlink.net> wrote:

> Is it possible to require a function argument to match alternative
> patterns and be optional at the same time? As a toy example, suppose I
> define
>
>     f[x:_Real|_Rational]:= x
>
> Is there a way make the argument optional with a given default value,
> say 0? I've tried this:
>
>     f[Optional[x:_Real|_Rational, 0]]:= x
>
> and various other combinations with no success. (Of course I could do
> this: f[]:= 0, but I'm interested in the more general issue.)
>
> -----
> Selwyn Hollis
> http://www.appliedsymbols.com
> (edit reply-to to reply)
>
>
>



-- 
DrBob at bigfoot.com
www.eclecticdreams.net


  • Prev by Date: Re: Optional Alternatives
  • Next by Date: Re: How to install Combinatorica
  • Previous by thread: Re: Optional Alternatives
  • Next by thread: Re: Optional Alternatives