开发者

List comprehension AND guards

开发者 https://www.devze.com 2023-04-11 03:38 出处:网络
How to implement AND guards in list comprehensions? Separating the guards with comma seems to word as OR:

How to implement AND guards in list comprehensions? Separating the guards with comma seems to word as OR:

1> rd(r, {a, b}开发者_C百科).
r
2> L = [#r{a = 1, b =2}, #r{a = 1, b = 3}].
[#r{a = 1,b = 2},#r{a = 1, b = 3}]
3> [X || X <- L, X#r.a =/= 1, X#r.b =/= 2].
[]

Thanks a lot.


That's definitely an AND. The first element fails both tests; the second fails the X#r.a =/= 1 test.

If you want OR, simply use the orelse operator:

2> [X || X <- L, X#r.a =/= 1 orelse X#r.b =/= 2].
[#r{a = 1,b = 3}]
0

精彩评论

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

关注公众号