Need help with avoiding lists in condition or pattern tests
How can we use a conditional or pattern test to make our function accept any symbols as input except for开发者_开发知识库 lists?
Use Except
:
f[x : Except[_List]] := doSomethingTo[x]
expr /. x : Except[_List] :> doSomethingElseTo[x]
You can combine that with Alternatives
(infix operator |
) to exclude several things:
g[x : Except[_List | _Rational]] := etc[x]
Edit: Consolidating answers from the comments too:
ListQ[expr]
will return True
if expr
is a list (has head List
) and False
otherwise. MatchQ[expr, _List]
and Head[expr]===List
are equivalent ways to accomplish the same thing.
精彩评论