Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #9445

Re: Pattern Syntax for Alternate Heads with Default Value

From Yi Wang <tririverwangyi@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Pattern Syntax for Alternate Heads with Default Value
Date 2012-04-05 09:59 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jljqef$d32$1@smc.vnet.net> (permalink)
References <jlh0n6$t6m$1@smc.vnet.net>

Show all headers | View raw


Hi, Ben,

Hope I understood your demand correctly. Try:

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

f[3] (* 9 *)
f[1.5] (* 2.25 *)
f[] (* 6.25 *)
f["hello"] (* f[hello] *)
f[Sin[x]] (* f[Sin[x]] *)

Cheers,
Yi

On Wednesday, April 4, 2012 4:28:22 AM UTC-4, 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

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Pattern Syntax for Alternate Heads with Default Value Ben <benp84@gmail.com> - 2012-04-04 08:28 +0000
  Re: Pattern Syntax for Alternate Heads with Default Value Murta <rodrigomurtax@gmail.com> - 2012-04-05 09:56 +0000
  Re: Pattern Syntax for Alternate Heads with Default Value Yi Wang <tririverwangyi@gmail.com> - 2012-04-05 09:59 +0000

csiph-web