Re: Pattern Syntax for Alternate Heads with Default Value
- To: mathgroup at smc.vnet.net
- Subject: [mg125869] Re: Pattern Syntax for Alternate Heads with Default Value
- From: Yi Wang <tririverwangyi at gmail.com>
- Date: Thu, 5 Apr 2012 05:53:44 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <jlh0n6$t6m$1@smc.vnet.net>
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