在Prolog中列表中生成K元素的组合时,如何忽略结果的第一匹匹配?

发布于 2025-01-30 06:42:24 字数 683 浏览 4 评论 0原文

当在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抱猫软卧 2025-02-06 06:42:24

根据您为什么想要的,有几种选择。

?- combination(3,[3,8,9,10,12,14],S).  % original question
   S = [3,8,9],     unexpected
;  S = [3,8,10]
;  S = [3,8,12]
;  ... .
?- dif(S,[3,8,9]), combination(3,[3,8,9,10,12,14],S).
   S = [3,8,10]
;  S = [3,8,12]
;  S = [3,8,14]
;  ... .
?- combination(3,[8,9,10,12,14],S).
   S = [8,9,10]
;  S = [8,9,12]
;  S = [8,9,14]
;  ... .
?- call_nth(combination(3,[3,8,9,10,12,14],S), Nth), Nth>1.
   S = [3,8,10], Nth = 2
;  S = [3,8,12], Nth = 3
;  S = [3,8,14], Nth = 4
;  ... .

首先,您可能不想要该特定解决方案,然后使用dif/2,或者只是使用较小的列表,最后,您想排除第一个答案(在这种情况下,这也是第一个答案解决方案),然后使用 call_nth/call_nth/call_nth/2 < /a>。

Depending on why you want this, there are several options.

?- combination(3,[3,8,9,10,12,14],S).  % original question
   S = [3,8,9],     unexpected
;  S = [3,8,10]
;  S = [3,8,12]
;  ... .
?- dif(S,[3,8,9]), combination(3,[3,8,9,10,12,14],S).
   S = [3,8,10]
;  S = [3,8,12]
;  S = [3,8,14]
;  ... .
?- combination(3,[8,9,10,12,14],S).
   S = [8,9,10]
;  S = [8,9,12]
;  S = [8,9,14]
;  ... .
?- call_nth(combination(3,[3,8,9,10,12,14],S), Nth), Nth>1.
   S = [3,8,10], Nth = 2
;  S = [3,8,12], Nth = 3
;  S = [3,8,14], Nth = 4
;  ... .

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 use call_nth/2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文