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: [mg125901] Re: Pattern Syntax for Alternate Heads with Default Value
  • From: Murray Eisenberg <murray at math.umass.edu>
  • Date: Fri, 6 Apr 2012 05:57:44 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com

While mathematically each integer is a real number, a Mathematica object 
with head Integer does _not_ have head Real. Thus:

   g[x_Real] := x^2
   g[3]
g[3]


On 4/5/12 5:51 AM, Murta wrote:
> On Apr 4, 5:28 am, Ben<ben... at gmail.com>  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
>
> Hi Ben
> Maybe something like that?
>
> f[x_ (Real | Integer)] := x^2; f[] = 6.25;
>
> (But all Integer are Real, I don't understand the application.
> It could be f[x_Real: 2.5] := x^2)
> best regards
> Murta
>

-- 
Murray Eisenberg                     murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305



  • Prev by Date: Error function integral / Wolfram Function reference
  • Next by Date: Evaluating Exponential functions
  • Previous by thread: Re: Pattern Syntax for Alternate Heads with Default Value
  • Next by thread: How to get the corresponding coefficients?