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


Groups > comp.soft-sys.math.mathematica > #9411 > unrolled thread

Pattern Syntax for Alternate Heads with Default Value

Started byBen <benp84@gmail.com>
First post2012-04-04 08:28 +0000
Last post2012-04-05 09:59 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  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

#9411 — Pattern Syntax for Alternate Heads with Default Value

FromBen <benp84@gmail.com>
Date2012-04-04 08:28 +0000
SubjectPattern Syntax for Alternate Heads with Default Value
Message-ID<jlh0n6$t6m$1@smc.vnet.net>
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


[toc] | [next] | [standalone]


#9439

FromMurta <rodrigomurtax@gmail.com>
Date2012-04-05 09:56 +0000
Message-ID<jljq8n$d0t$1@smc.vnet.net>
In reply to#9411
On Apr 4, 5:28 am, Ben <ben...@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

[toc] | [prev] | [next] | [standalone]


#9445

FromYi Wang <tririverwangyi@gmail.com>
Date2012-04-05 09:59 +0000
Message-ID<jljqef$d32$1@smc.vnet.net>
In reply to#9411
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web