在Prolog中列表中生成K元素的组合时,如何忽略结果的第一匹匹配?
当在Prolog中生成k元素的组合时,如何忽略结果的第一匹匹配?
%是代码。
combination(0,_,[]).
combination(N,[X|T],[X|Comb]):-N>0,N1 is N-1,combination(N1,T,Comb).
combination(N,[_|T],Comb):-N>0,combination(N,T,Comb).
?- combination(3,[3,8,9,10,12,14],S).
%result will be.
S = [3, 8, 9] ;
S = [3, 8, 10] ;
S = [3, 8, 12] ;
S = [3, 8, 14] ;
S = [3, 9, 10] ;
S = [3, 9, 12] ;
S = [3, 9, 14] ;
S = [3, 10, 12] ;
S = [3, 10, 14] ;
S = [3, 12, 14] ;
S = [8, 9, 10] ;
S = [8, 9, 12] ;
S = [8, 9, 14] ;
S = [8, 10, 12] ;
S = [8, 10, 14] ;
S = [8, 12, 14] ;
S = [9, 10, 12] ;
S = [9, 10, 14] ;
S = [9, 12, 14] ;
S = [10, 12, 14] ;
false.
%I want to ignore the first match [3,8,9].
how to ignore the first match of a result when generating the combination of K elements in a list in prolog ?
%here is the code.
combination(0,_,[]).
combination(N,[X|T],[X|Comb]):-N>0,N1 is N-1,combination(N1,T,Comb).
combination(N,[_|T],Comb):-N>0,combination(N,T,Comb).
?- combination(3,[3,8,9,10,12,14],S).
%result will be.
S = [3, 8, 9] ;
S = [3, 8, 10] ;
S = [3, 8, 12] ;
S = [3, 8, 14] ;
S = [3, 9, 10] ;
S = [3, 9, 12] ;
S = [3, 9, 14] ;
S = [3, 10, 12] ;
S = [3, 10, 14] ;
S = [3, 12, 14] ;
S = [8, 9, 10] ;
S = [8, 9, 12] ;
S = [8, 9, 14] ;
S = [8, 10, 12] ;
S = [8, 10, 14] ;
S = [8, 12, 14] ;
S = [9, 10, 12] ;
S = [9, 10, 14] ;
S = [9, 12, 14] ;
S = [10, 12, 14] ;
false.
%I want to ignore the first match [3,8,9].
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您为什么想要的,有几种选择。
首先,您可能不想要该特定解决方案,然后使用
dif/2
,或者只是使用较小的列表,最后,您想排除第一个答案(在这种情况下,这也是第一个答案解决方案),然后使用call_nth/call_nth/call_nth/2
< /a>。Depending on why you want this, there are several options.
First, you might not want that particular solution, then use
dif/2
, or maybe just use a smaller list, and finally, you want to exclude the first answer (which in this case is also the first solution), then usecall_nth/2
.