序言:过滤列表? [英] Prolog: Filtering a list?
问题描述
我目前正在研究Prolog的一个非常短的项目,并试图将我创建的过滤器应用到列表中。我有你可以称之为过滤器准备好,但我不能应用它。 (B,B)
...如果符合某些条件,则输出true。 code> filterList(A,[X,Y,Z])
...输出包含第二个参数中所有使过滤器输出 false 的元素的列表。 (所以如果filter(A,X)为true,输出是[Y,Z])。
我已经准备好了filter函数,但是现在我需要将其应用于第二个示例中所示的列表,排除过滤器在应用第一个参数时返回true的所有元素。因此,如果过滤器是一个简单的A == B,该函数应该接收A [A,B,A,C,D,A]并输出[B,C,D],显然已经删除了过滤器的所有元素。
我遇到了函数的基本结构如果任何人都可以提供这样的功能的基本轮廓,那将是非常有帮助的。我尽可能地简化了我的情况,这样我就可以随心所欲地提供和修改。
预先感谢!
如果您在Prolog中搜索更高级的函数,您应该定义一下 Naish(1995),这是一个很好的资源。
他定义的 filter / 3
如下(他使用差异表示符号,因此escape必须定义 filter / b
$ b $
$($,$,$ / $ c $) [])。
filter(P,A0-As0,As): -
(
call(P,A0) - > As = A0-As1
;
As = As1
)
,过滤(P,As0,As1)。
我对这个谓词有疑问,请在评论中问我。阅读这篇论文也强烈建议,也可以定制 I'm currently working on a very short project on Prolog, and just got stuck trying to apply a "filter" I have created to a list. I have what you could call the filter ready, but I can't apply it. It'd be better if I illustrate: ...outputs 'true' if certain conditions are met. ...outputs a list which includes all elements from the second argument that make the filter output false. (So if filter(A, X) is true, the output is [Y, Z] ). I have the "filter" function ready, but now I need to apply it to a list as shown on the second example, excluding all elements for which the filter returns true when applied with the first argument. So, if the filter is a simple A == B, the function is supposed to receive A [A,B,A,C,D,A] and output [B,C,D], having removed all the elements for which the filter applies, obviously. I'm having trouble with the basic structure of the function, so if anyone could supply a basic outline for a function such as this it would be of great help. I've simplified my situation as much as possible so I can take whatever you may be able to provide and modify it for my needs. Thanks in advance! If you are searching for higher-order functions in Prolog, you should definetly consult Naish (1995), a very good resource on this. His definition of map
, foldr
和撰写
!请注意,他提到的许多限制(例如缺少
SWI-Prolog有调用/ 3
或更高阶 = ..
运算符,它解决了他所有的问题,并且使任意的n阶逻辑成为可能。filter(A, B)
filterList(A, [X, Y, Z])
filter/3
is the following (he uses difference-list notation, therefore escapes having to define filter/4
):
filter(_,[],[]).
filter(P, A0-As0, As) :-
(
call(P, A0) -> As = A0-As1
;
As = As1
)
, filter(P, As0, As1).
I you have questions about this predicate, please ask me in the comment. Reading the paper is also highly recommended, it also definess map
, foldr
and compose
! Note that many of the limitations he mentions (like, for example a missing call/3
or a higher-order apply
do not apply anymore. SWI-Prolog has the =..
operator, which addresses all of his concerns and makes arbitrary n-order logic possible.
这篇关于序言:过滤列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!