5
$\begingroup$

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.

$\endgroup$
1
  • 1
    $\begingroup$ ref / Select / Generalizations&Extensions / #1. $\endgroup$
    – Kuba
    Jan 11, 2019 at 7:58

1 Answer 1

7
$\begingroup$
  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:

$\endgroup$
1
  • 3
    $\begingroup$ 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. $\endgroup$
    – Theo Tiger
    Jan 11, 2019 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.