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: [mg125880] Re: Pattern Syntax for Alternate Heads with Default Value
  • From: Fred Simons <f.h.simons at tue.nl>
  • Date: Thu, 5 Apr 2012 05:57:35 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201204040827.EAA29886@smc.vnet.net>

The correct syntax for an argument is  name : pattern : default value.

When the pattern starts with an underscore, we may leave out the first 
colon (have a look when you enter x:_)

So this seems to be what you are looking for:

f[x : (_Real | _Integer) : 2.5] := x^2

Best wishes,

Fred Simons
Eindhoven University of Technology

Op 4-4-2012 10:27, Ben schreef:
> 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
>
>
>
>
>
> -----
> Geen virus gevonden in dit bericht.
> Gecontroleerd door AVG - www.avg.com
> Versie: 2012.0.1913 / Virusdatabase: 2114/4913 - datum van uitgifte: 04/03/12
>
>
>




  • Prev by Date: Re: addRule
  • Next by Date: Re: How to generate ``nice'' algebra output from command-line mathematica?
  • Previous by thread: Re: Pattern Syntax for Alternate Heads with Default Value
  • Next by thread: Re: Pattern Syntax for Alternate Heads with Default Value