Look at these two examples:
In[1]:=Select[f[i], MatchQ[_f]]
Out[1]:=f[]
In[2]:=Cases[f[i],_f]
Out[2]:={}
It seems that the Select take $f[i]$ as List, but Cases do not.
I also can not understand why the first gives that answer.
Thanks.
Look at these two examples:
In[1]:=Select[f[i], MatchQ[_f]]
Out[1]:=f[]
In[2]:=Cases[f[i],_f]
Out[2]:={}
It seems that the Select take $f[i]$ as List, but Cases do not.
I also can not understand why the first gives that answer.
Thanks.
Select
preserves the head of the original expression, while Cases
always returns the result in a List
.
Select
operates only at level one, whereas Cases
accepts a levelspec.
Specifically your first output is equivalent to these:
Part[f[i], {}]
Delete[f[i], 1]
The second can be made to match by expanding the levelspec to include level zero:
Cases[f[i], _f, {0, 1}]
{f[i]}
Recommended reading:
Select[f[i], MatchQ[_f]]
matches nothing and only returns an empty expression with head f
. It is the same as Select[f[i], MatchQ[_somethingelse]]
. The recommended answers are very informative btw.
$\endgroup$
Commented
Jan 11, 2019 at 10:04