Re: Optional Alternatives
- To: mathgroup at smc.vnet.net
- Subject: [mg50025] Re: [mg50010] Optional Alternatives
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Thu, 12 Aug 2004 05:43:36 -0400 (EDT)
- References: <200408101002.GAA19525@smc.vnet.net> <200408110953.FAA04123@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 11 Aug 2004, at 11:53, Selwyn Hollis wrote: > *This message was transferred with a trial version of CommuniGate(tm) > Pro* > 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) > > There is no problem as long as your optional value matches your pattern. Thus the following will work: In[1]:= f[Optional[x:_Real|_Rational, 0.]]:= x In[2]:= f[] Out[2]= 0. The same with 0 in place of 0 will not. Andrzej Kozlowski Andrzej Kozlowski Chiba, Japan http://www.mimuw.edu.pl/~akoz/
- References:
- 50,000th Message
- From: "Steven M. Christensen" <steve@smc.vnet.net>
- Optional Alternatives
- From: Selwyn Hollis <sh2.7183@misspelled.erthlink.net>
- 50,000th Message