开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜