Why Cases and Select treat the List differently?

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.

• ref / Select / Generalizations&Extensions / #1. – Kuba Jan 11 at 7:58

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]}


• 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 at 10:04