Re: pattern test and default value
- To: mathgroup at smc.vnet.net
- Subject: [mg5979] Re: [mg5967] pattern test and default value
- From: Allan Hayes <hay at haystack.demon.co.uk>
- Date: Sat, 8 Feb 1997 22:37:53 -0500
- Sender: owner-wri-mathgroup at wolfram.com
marliesb at sci.kun.nl (Marlies) [mg5967] pattern test and default value writes > It is possible to do the following: > > x_h:v (an expression with head h and default value v) > > Can anyone tell me whether it is possible to have a pattern test > like > _?NumberQ AND have a certain default value? Marlies: Yes to both. (1) g[x_h:h[v]]:= x^2 (*default must match x_h*) g[h[4]] 2 h[4] g[] 2 h[v] (2) Use form p[x:(_?NumberQ): 5] := x^2 (*default must satisfy test*) p[7] 49 p[] 25 Parsing problems with some other problems can be seen with FullForm: FullForm[x:(_?NumberQ):5] (*the form that works*) Optional[Pattern[x, PatternTest[Blank[], NumberQ]], 5] FullForm[x:_?NumberQ:5] Pattern[x, PatternTest[Blank[], Pattern[NumberQ, 5]]] FullForm[x_?NumberQ:5] PatternTest[Pattern[x, Blank[]], Pattern[NumberQ, 5]] It's sometimes quicker to type in the full form Allan Hayes hay at haystack.demon.co.uk http://haystack.demon.co.uk