5

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.

XiaoaiX
  • 397
  • 1
  • 5

1 Answers1

7
  1. Select preserves the head of the original expression, while Cases always returns the result in a List.

  2. 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:

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 3
    The essence of (1) is that 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. – Theo Tiger Jan 11 '19 at 10:04