Select[{1, 2, 4, 7, 6, 2}, # > 2 &]
Gives {4, 7, 6}. OK!
So one should think that the following would work:
myList = {-2, -1, 0, 1, 2};
FirstPosition[myList, # > 0 &]
But this fails. The correct command is
myList = {-2, -1, 0, 1, 2};
FirstPosition[myList, _?(# > 0 &)]
But why on earth do we have to use different syntax for those two cases???
Why is the _?
necessary?
As it was commented by MarcoB:
Select
takes a criterion function, while FirstPosition
takes a pattern instead.
Can someone give a for dummies explanation what is the difference between both? Literally I would say a criterion is a pattern and a pattern is a criterion...
Select
takes a criterion function as an argument, not a pattern;FirstPosition
takes a pattern instead. You are converting the criterion function fromSelect
into a conditional pattern. On the other hand,Cases
is similar toSelect
in functionality, but it takes a pattern argument instead. That's just how those functions were designed! $\endgroup$Select[]
came waay beforeFirstPosition[]
. Now, why they chose to patternFirstPosition[]
afterCases[]
rather thanSelect[]
is a question for the language designers. $\endgroup$Position
also takes patterns. AlsoSelectFirst
andFirstCase
are things too. $\endgroup$Select
with criterions andCases
with patterns to get the elements of a list. We havePosition
with patterns to get the indices. Where's myCriterionPosition
with criterions??? Also, what's a "gedanken version"? $\endgroup$