列表理解和防护
如何在列表推导式中实现 AND
保护?用逗号分隔守卫似乎是 OR
:
1> rd(r, {a, b}).
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].
[]
非常感谢。
How to implement AND
guards in list comprehensions? Separating the guards with comma seems to word as OR
:
1> rd(r, {a, b}).
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这绝对是一个 AND。第一个元素未通过这两项测试;第二个未通过
X#ra =/= 1
测试。如果您想要 OR,只需使用 orelse 运算符:
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: