I have a list whose elements are (symbol or list) and I would like to replace the inner lists by their first elements. I do this
f[list_] := Replace[ list , x_List -> First[x], 1 ]
If I test it
f[{{a,b},c}]
I get expected result
{a,c}
But I get an error too
First::normal: "Nonatomic expression expected at position 1 in First[x].
Why do I get the error?
edit: I can alter the definition of f
such that I do not get the error, but I still do not understand why I got the error in the first place.
version 1:(based on J.M. comment)
f[list_] := Replace[list, x_List :> x, 1]
version 2:
f[list_] := Replace[list, {x_, (_) ...} -> x, 1]
:>
instead of->
. $\endgroup$First
to elementc
like suchFirst @ c
sincec
is an atomic element there is nothing to applyFirst
to $\endgroup$c
should not match the patternx_List
or does it match? $\endgroup$Rule
instead ofRuleDelayed
. One evaluates its right-hand side at once; the other doesn't. $\endgroup$Background & Context
. 2nd paragraph $\endgroup$