将 X,Y 与 (1,2), (1,-2), (-1,2), (-1,-2), (2,1), (2) 统一的优雅方法是什么,-1) , (-2,1), (-2,-1)?
将 X,Y 与 (1,2), (1,-2), (-1,2), (-1,-2), (2,1), (2,-1) 统一的优雅方法是什么, (-2,1), (-2,-1)?
这样做似乎容易出错且乏味:
foo(1,2).
foo(1,-2).
foo(-1,-2).
...
...
...
而且这种方式似乎太昂贵了:
foo(X,Y) :-
L = [1,-1,2,-2],
member(X,L),
member(Y,L),
abs(X,X1), abs(Y,Y1),
X1 =\= Y1.
What's an elegant way to unify X,Y with (1,2), (1,-2), (-1,2), (-1,-2), (2,1), (2,-1) , (-2,1), (-2,-1)?
Doing it this way seems error prone and tedious:
foo(1,2).
foo(1,-2).
foo(-1,-2).
...
...
...
And this way seems too expensive:
foo(X,Y) :-
L = [1,-1,2,-2],
member(X,L),
member(Y,L),
abs(X,X1), abs(Y,Y1),
X1 =\= Y1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对评论内容的进一步发展:
嗯嗯……看,写所有案例更容易、更短 xD
A further development on what was commented:
Hmmmmmm... look, it's easier and shorter to just write all the cases xD
以这种方式使用member/2 是一种Prolog 反模式。虽然
member/2 很短,通常member/2 不能做子句索引。
您可以自己尝试并比较这两个解决方案:
Foo Member:
Foo Clause:
Now run it in GNU Prolog:
好吧,如果当第二个参数为理由时,某些 Prolog 开始自动将 member/1 编译为子句,情况可能会发生变化。
Using member/2 this way is kind of a Prolog anti pattern. Although
member/2 is short, usually member/2 cannot do clause indexing.
You can try your self and compare these two solutions:
Foo Member:
Foo Clause:
Now run it in GNU Prolog:
Well, the situation might change if some Prolog starts automatically compiling member/1 into clauses, when the second argument is ground.