Say I have Part
of a variable x
.
Part[x, 1]
Then, I'll get an error message, as the variable x
is not recognised as a list with multiple parts.
If I want to suppress this error, because I know for sure x
will have multiple parts, I can use the Quiet
function.
Quiet[Part[x, 1], {Part::partd}]
But now imagine that the list variable x
itself is an element of another list y
such that x=y[[1]]
.
Quiet[Part[Part[y, 1], 1], {Part::partd}]
I expect this to return y[[1][[1]]
, however, what I get is y
.
Any ideas why this happens? Obviously the examples are simplified working examples, but it should work. How can I get my expected behaviour?
Indexed
instead ofPart
. See also here. $\endgroup$y
becausePart[y, 1]
doesn't evaluate ify
is atomic. So the outerPart
then extracts the first argument ofPart[y,1]
, which isy
. This is becausePart
works on arbitrary expression, not justList
s $\endgroup$Evaluate
around the innerPart
doesn't seem to work. TheIndexed
solution is much nicer anyway, as it also doesn't require theQuiet
. $\endgroup$Evaluate
will not do anything here since there's no hold attribute to override (see also mathematica.stackexchange.com/a/180500/43522) . Mathematica tries to evaluatePart[y, 1]
, but since there's nothing it can evaluate to, it stays the way it is. That's how Mathematica handles most ill-defined expressions: it's part of the symbolic nature of the language. Mathematica uses a term-rewriting style of evaluation and when there's no applicable rules available, things just stay the way they are. $\endgroup$