Part specifiers in a list.
- To: mathgroup at christensen.cybernetics.net
 - Subject: [mg296] Part specifiers in a list.
 - From: kaufman1 at llnl.gov (Al Kaufman)
 - Date: Tue, 6 Dec 1994 09:21:30 -0800
 
Steve Chorcoran writes:
>If I have a list like,
>
>list = {1,1,1,2}
>
>how do I find the part of a expression at the level specified by the list?
>i.e.
>
>I want exp[[1,1,1,2]],
>
>but exp[[list]] doesn't work.
>
To get an expression specified by a list, one can use:
Part[exp,Sequence@@list] which removes the Head List from List and
replaces it by the undocumented head Sequence.  Sequence automatically
flattens and is absorbed by any other head.  So that:
f[a___,Sequence[b___] -> f[a,b].
Thus, one can define the operation:
ListPart[e_,l_List] := Part[e,Sequence@@l];
One might note that the Mma operation:
Position[e,pat] gives a list of positions of the pattern, pat, in e.
ListPart can be used to extract the items which match the pattern via
ExpressionList[e_,pat_] := Map[ListPart[e,#]&,Position[e,pat]]
================================
Al Kaufman
Lawrence Livermore National Lab
Mail stop L-83
Livermore, Ca  94550
Phone:  510-422-1599
FAX:    510-422-8471
E-mail: <kaufman1 at llnl.gov>
Alt. e-mail: <nutronstar at aol.com>