Re: Impossible?
- To: mathgroup@smc.vnet.net
- Subject: [mg10504] Re: [mg10458] Impossible?
- From: Daniel Lichtblau <danl@wolfram.com>
- Date: Tue, 20 Jan 1998 02:22:52 -0500
- References: <199801160934.EAA08231@smc.vnet.net.>
Arturas Acus wrote:
>
> Hello,
>
> I would like to construct a function which gives me an absolute Level of
> subexpression in expression. For example I want:
>
> a*(b+c*(d+AbsoluteLevelQ[e]))
>
> evaluate to
>
> Level[a*(b+c*(d+e)),e].
> ( AbsoiuteLevelQ climbs up until reaches In[], after that calls Level
> )
>
> More generally, I would like to control absolute level (level in respect
> to In[%]) at which patter matching take place. For example if pattern
> matches at Level n>3, then rule is applied, but if it matches at
> Level n<3 then not.
>
> I suspect, that evaluation machinery here is hardly involved. Do
> Mathematica language provides tools for such a control? Please comment
> if solution don't exist.
>
> Arturas Acus
> ....
Something along the following lines might get you started.
ruls = {f_[e1___, al[e_Symbol], e2___] :> al[f[e1,e,e2], e],
f_[e1___, al[e2_,e_Symbol], e3___] :> al[f[e1,e2,e3], e]}
In[81]:= (a*(b+c*(d+al[e])) //. ruls) /. al->Level Level::level: Level
specification e is not of the form n, {n}, or {m, n}.
Out[81]= Level[a (b + c (d + e)), e]
But, while this gives the output you specified, I doubt it is what you
want. Maybe you have in mind a nesting-depth-counter? Could do
In[1]:= ruls2 = {al[e_] :> al[e,1],
f_[e1___, al[e2_,n_Integer], e3___] :> al[f[e1,e2,e3], n+1]}
In[2]:= (a*(b+c*(d+al[e])) //. ruls2) Out[2]= al[a (b + c (d + e)), 5]
or perhaps
FindLevel[e_] := Module[{pos, len},
len = Length[pos = Position[e, al[_]]];
Length[pos[[1]]] +1 /; len==1
]
In[16]:= FindLevel[a*(b+c*(d+al[e]))] Out[16]= 5
Daniel Lichtblau
Wolfram Research
- References:
- Impossible?
- From: "Arturas Acus" <acus@itpa.lt>
- Impossible?