Re: Re: DownValues and Cases

*To*: mathgroup at smc.vnet.net*Subject*: [mg70802] Re: [mg70788] Re: [mg70759] DownValues and Cases*From*: Andrzej Kozlowski <akoz at mimuw.edu.pl>*Date*: Sat, 28 Oct 2006 05:21:39 -0400 (EDT)*References*: <200610260639.CAA19605@smc.vnet.net> <200610270428.AAA24514@smc.vnet.net> <4541F45C.2040407@wolfram.com>

On 27 Oct 2006, at 20:58, Carl Woll wrote: > Andrzej Kozlowski wrote: > >> On 26 Oct 2006, at 15:39, Bruce Colletti wrote: >> >> >>> Re Mathematica 5.2 under WinXP. >>> >>> The results of the first two Cases statements leads me to >>> anticipate {{1,3.5}, {2,5}, {_,0}} as the output of the third. >>> >>> The output of the fourth Cases statement confirms this expectation. >>> >>> Unfortunately, the output of the third statement is the empty list. >>> >>> Why so? I've reviewed a related mid-July MathGroup thread, but >>> don't see the answer (if it's there at all). >>> >>> Thankx. >>> >>> Bruce >>> >>> -------------------------- >>> >>> f at 1=3.5; >>> f@2=5; >>> f[_]:=0; >>> h=DownValues@f >>> >>> Out[4]={HoldPattern[f[1]] :> 3.5, HoldPattern[f[2]] :> 5, >>> HoldPattern[f[_]] :> 0} >>> >>> Cases[h,HoldPattern[f[x_]] -> x,Infinity] >>> Out[5]={1,2,_} >>> >>> Cases[h,(_ :> y_) -> y,Infinity] >>> Out[6]={3.5,5,0} >>> >>> Cases[h,(HoldPattern[f[x_]] :> y_) -> {x,y},Infinity] >>> Out[7]={} >>> >>> Cases[{a :> 4,b :> 5, c :> 6},(x_ :> y_) -> {x,y}] >>> Out[8]={{a,4},{b,5},{c,6}} >>> >>> >> >> This is rather tricky. The problem is how to force the >> PatternMatcher to interpret HoldPattenr literally rather than as >> the pattern to be held. Note that: >> >> Cases[h, (Verbatim[HoldPattern[f[2]]] :> y_) -> {x, y}, >> Infinity] >> >> {{x, 5}} >> >> works, but: >> >> >> Cases[h, (Verbatim[HoldPattern[f[x_]]] :> y_) -> {x, y}, >> Infinity] >> >> {} >> >> doesn't, because now x_ is also interpreted literally rather than >> as a pattern. > In this case you can apply Verbatim to just the head HoldPattern, > but then you still need a HoldPattern to prevent f[x_] from > evaluating. So: > > In[18]:= > Cases[h, (Verbatim[HoldPattern][HoldPattern[f[x_]]] :> y_) -> {x, > y}, Infinity] > Out[18]= > {{1, 3.5}, {2, 5}, {_, 0}} > > Carl Woll > Wolfram Research > Yes, of course! . I actually thought about wrapping Verbatim around the Head, but then realized that f[x] would then evaluate and at this point gave up the idea. It did not occur to me to use a second HoldPattern. Andrzej Kozlowski

**References**:**DownValues and Cases***From:*Bruce Colletti <vze269bv@verizon.net>

**Re: DownValues and Cases***From:*Andrzej Kozlowski <akoz@mimuw.edu.pl>