MathGroup Archive 2004

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

Search the Archive

Re: Optional Alternatives

  • To: mathgroup at smc.vnet.net
  • Subject: [mg50047] Re: [mg50010] Optional Alternatives
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Fri, 13 Aug 2004 05:55:40 -0400 (EDT)
  • Reply-to: hanlonr at cox.net
  • Sender: owner-wri-mathgroup at wolfram.com

The default value must be consistent with your Condition, that is, its Head 
must be Real or Rational

Clear[f];

f[Optional[x:_Real|_Rational,0.]]:=x

f[]

0.

f/@{2,2.,1/2,x}

{f[2], 2., 1/2, f[x]}


Bob Hanlon

> 
> From: Selwyn Hollis <sh2.7183 at misspelled.erthlink.net>
To: mathgroup at smc.vnet.net
> Date: 2004/08/11 Wed AM 05:53:08 EDT
> To: mathgroup at smc.vnet.net
> Subject: [mg50047] [mg50010] Optional Alternatives
> 
> 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)
> 
> 


  • Prev by Date: How does a notebook get its own filename or directory?
  • Next by Date: Re: Re: Re: How to set linewidth in 3-D graphics?
  • Previous by thread: Re: Optional Alternatives
  • Next by thread: Smalest enclosing circle