从序言中的列表中过滤掉大量数字

发布于 2024-10-06 00:44:22 字数 243 浏览 2 评论 0原文

我想编写一个函数,通过删除小于或等于特定数字的所有内容来过滤数字列表。该函数将采用两个参数:数字列表和要过滤的数字。该函数应返回一个列表,其中包含大于过滤器编号的所有数字。

有时像这样:

filter_num_list(L1,N,L2) :- ...

test_filter_num_list :- filter_num_list([1,2,3,4,5,6,7,8,9],5,[5,6,7,8,9]).

I want to write a function which filters a list of numbers by removing everything less than or equal to a specific number. The function will take two parameters: a list of numbers and the number to filter. The function should returns a list which has all the numbers larger than the filter number.

Sometime like this:

filter_num_list(L1,N,L2) :- ...

test_filter_num_list :- filter_num_list([1,2,3,4,5,6,7,8,9],5,[5,6,7,8,9]).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

薄荷→糖丶微凉 2024-10-13 00:44:22

另请参见库谓词,例如 include/3 和 except/3:

?- include(=<(5), [1,2,3,4,5,6,7,8,9], Is).
Is = [5, 6, 7, 8, 9].

See also library predicates like include/3 and exclude/3:

?- include(=<(5), [1,2,3,4,5,6,7,8,9], Is).
Is = [5, 6, 7, 8, 9].
梦幻的味道 2024-10-13 00:44:22

使用 tfilter/3 和具体化的 约束(# <)/3,您可以跟上并立即表达您想要的内容!

:- use_module(library(clpfd)).

这是我使用 SWI-Prolog 版本 7.1.37 运行的查询:

?- tfilter(#<(5),[1,2,3,4,5,6,7,8,9],Xs).
Xs = [6,7,8,9].                             % succeeds deterministically
false.

因为代码是 单调,我们还可以提出更一般性的问题并得到逻辑上合理的答案。

?- tfilter(#<(7),[A,B,C],Xs).
Xs = [],      A in inf..7, B in inf..7, C in inf..7 ;
Xs = [C],     A in inf..7, B in inf..7, C in 8..sup ;
Xs = [B],     A in inf..7, B in 8..sup, C in inf..7 ;
Xs = [B,C],   A in inf..7, B in 8..sup, C in 8..sup ;
Xs = [A],     A in 8..sup, B in inf..7, C in inf..7 ;
Xs = [A,C],   A in 8..sup, B in inf..7, C in 8..sup ;
Xs = [A,B],   A in 8..sup, B in 8..sup, C in inf..7 ;
Xs = [A,B,C], A in 8..sup, B in 8..sup, C in 8..sup ;
false.

With tfilter/3 and the reified constraint (#<)/3, you can keep up and express what you want in no time!

:- use_module(library(clpfd)).

Here's a query that I ran with SWI-Prolog version 7.1.37:

?- tfilter(#<(5),[1,2,3,4,5,6,7,8,9],Xs).
Xs = [6,7,8,9].                             % succeeds deterministically
false.

As the code is monotone, we can also ask more general queries and get logically sound answers.

?- tfilter(#<(7),[A,B,C],Xs).
Xs = [],      A in inf..7, B in inf..7, C in inf..7 ;
Xs = [C],     A in inf..7, B in inf..7, C in 8..sup ;
Xs = [B],     A in inf..7, B in 8..sup, C in inf..7 ;
Xs = [B,C],   A in inf..7, B in 8..sup, C in 8..sup ;
Xs = [A],     A in 8..sup, B in inf..7, C in inf..7 ;
Xs = [A,C],   A in 8..sup, B in inf..7, C in 8..sup ;
Xs = [A,B],   A in 8..sup, B in 8..sup, C in inf..7 ;
Xs = [A,B,C], A in 8..sup, B in 8..sup, C in 8..sup ;
false.
叹梦 2024-10-13 00:44:22

尝试类似的东西:

filter_num_list([],N,[]) :- true.  
filter_num_list([H|T],N,[H|S]) :- H > N,filter_num_list(T,N,S).  
filter_num_list([H|T],N,S) :- N >= H, filter_num_list(T,N,S).

try something like:

filter_num_list([],N,[]) :- true.  
filter_num_list([H|T],N,[H|S]) :- H > N,filter_num_list(T,N,S).  
filter_num_list([H|T],N,S) :- N >= H, filter_num_list(T,N,S).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文