Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2012

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

Search the Archive

Re: Pattern Syntax for Alternate Heads with Default Value

  • To: mathgroup at smc.vnet.net
  • Subject: [mg125874] Re: Pattern Syntax for Alternate Heads with Default Value
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Thu, 5 Apr 2012 05:55:29 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com

On 4/4/12 at 4:27 AM, benp84 at gmail.com (Ben) wrote:

>I'm trying to build a unary function which accepts only arguments
>with two possible heads and has a default value.  That is, something
>like the following, except that this doesn't work.  Anyone know how
>to do this correctly?

>f[(x_Real | x_Integer) : 2.5] := x^2; where f[3] = 9 and f[1.5] =
>2.25 and f[] = 6.25

In[16]:= f[x_?(# \[Element] Reals &)] := x^2
f[x_] := 6.25

In[19]:= Clear[y]; f[y]

Out[19]= 6.25

In[20]:= f[.8]

Out[20]= 0.64

No need to test for integers since reals include integers.




  • Prev by Date: Re: addRule
  • Next by Date: Re: addRule
  • Previous by thread: Re: Pattern Syntax for Alternate Heads with Default Value
  • Next by thread: Re: Pattern Syntax for Alternate Heads with Default Value