[Date Index]
[Thread Index]
[Author Index]
Re: Mutiple delete in lists
*To*: mathgroup at CHRISTENSEN.CYBERNETICS.NET
*Subject*: [mg374] Re: [mg360] Mutiple delete in lists
*From*: Dana_Scott at POP.CS.CMU.EDU
*Date*: Wed, 28 Dec 94 13:41:11 EST
The problem in understanding selections is that there is an
inconsitency in the design of Mathmeatica. You should have looked at
the on-line help to check the syntax. We find:
In[1]:=
??Delete
Delete[expr, n] deletes the element at position n in expr. If n
is negative, the position is counted from the end.
Delete[expr, {i, j, ...}] deletes the part at position {i, j,
...}. Delete[expr, {{i1, j1, ...}, {i2, j2, ...}, ...}]
deletes parts at several positions.
Attributes[Delete] = {Protected}
This means that the "official" notation for a delete of the nth term
of an expression is actually Delete[expr, {{n}}]. This is clearly too
tiresome to type, so we abbreviate it as Delete[expr, n] with a cute
expansion of meaning to the negative numbers (as in Drop).
Now a list such as {i, j, ...} is a PATH in a "tree". This is nice,
since all expressions are of the form Head[Part1, Part2, ...], where
each Parti is again of the same form until we come to a symbol (=
atom). Therefore, {i, j, ...} means to take the ith part of the
expression, then take the jth part of that, and then the next part as
indicated, and so on. Hence, the offical notation for removing a
deeply embedded subtree is Delete[expr, {{i, j, ...}}], but again, two
curlies are too much to type, so we use an abbrivated form
Delete[expr, {i, j, ...}].
Finally, when we want to drop a whole batch of subtrees, we just have
to go ahead and type Delete[expr, {{i1, j1, ...}, {i2, j2, ...},...}].
So here is what you wanted to do first:
In[2]:=
Delete[Range[10], {{2},{3}}]
Out[2]=
{1, 4, 5, 6, 7, 8, 9, 10}
But if we have a real tree and not just a flat list, then we get a
result like this:
In[3]:=
Delete[{{1,2},{3,4}}, {2,1}]
Out[3]=
{{1, 2}, {4}}
Understand?? Well, now look at the explanation of Part:
In[9]:=
??Part
expr[[i]] or Part[expr, i] gives the ith part of expr. expr[[-i]]
counts from the end. expr[[0]] gives the head of expr.
expr[[i, j, ...]] or Part[expr, i, j, ...] is equivalent to
expr[[i]] [[j]] .... expr[[ {i1, i2, ...} ]] gives a list of
the parts i1, i2, ... of expr.
Attributes[Part] = {Protected}
This shows a QUITE DIFFERENT convention of using curlies that does not
agree with the Delete conventions at all. Moreover, there is no
simple notation for getting a list of several subtrees.
Look, WRI, if Delete gives an expression with a selection of subtrees
removed, then Part should have a facility for exactly getting just the
subtrees that were removed. There ought to be a natural ecology of
expressions as trees! And if we can write Part[expr, i, j, ...] for
moving down a path, we ought to have been able to write something like
Delete[expr, i, j, ...] for the corresponding destruction operation.
Do people agree that there is a design fault here?
Prev by Date:
**Re: Suggestions needed for Mathematica course**
Next by Date:
**Re: Simple Solve Question**
Previous by thread:
**Blackman tutorial/supplement**
Next by thread:
**Re: Simple Solve Question**
| |