开发者

Need help with avoiding lists in condition or pattern tests

开发者 https://www.devze.com 2023-02-22 01:46 出处:网络
How can we use a conditional or pattern test to make our function accept any symbols as input except for开发者_开发知识库 lists?Use Except:

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

精彩评论

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