Re: Patterns and default arguments
- To: mathgroup at smc.vnet.net
- Subject: [mg28146] Re: [mg28113] Patterns and default arguments
- From: BobHanlon at aol.com
- Date: Sun, 1 Apr 2001 00:08:06 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
r = Range[3]; The most obvious way is to use two statements Clear[f]; f[x1_] := f[x1, {}]; f[x1_, x2:{_Integer...}] := x1*x2; {f[x], f[x, {}], f[x, r], f[x, N[r]]} {{}, {}, {x, 2*x, 3*x}, f[x, {1., 2., 3.}]} You can also use explicit conditions Clear[f]; f[x1_, x2_List:{}] := x1*x2 /; And @@ (IntegerQ /@ x2); {f[x], f[x, {}], f[x, r], f[x, N[r]]} {{}, {}, {x, 2*x, 3*x}, f[x, {1., 2., 3.}]} For f[z_Integer] := z^2 implemented using Function Function[z, If[IntegerQ[z], z^2]][#]& /@ {5, 5.} {25, Null} or If[IntegerQ[#], #^2]&[#]& /@ {5, 5.} {25, Null} Bob Hanlon In a message dated 2001/3/31 3:38:10 AM, jeff at lheapop.gsfc.nasa.gov writes: >I am trying to combine a pattern with a default argument in a delayed >set expression and it is not working. > >I can make a default argument work like this: f[x1_, x2_List:{}]:=... > >And the pattern works like this: f[x1_, x2:{_Integer...}]:= > >But if I combine the two like this: f[x1_, x2:{_Integer...}:{}]:= >Mathematica accepts it without error and the pattern expression works >but the default does not. > >Am I using the wrong syntax or can the two just not be combined? > > >Also, is there a way to specify a pattern for aguments to something >defined as a function (i.e. 'Function[...]') rather than a delayed set? > >I have to use 'Function' or ()& in order for the head to be 'Function' >rather than 'Symbol'. >