如何使用findall/3(仅使用一个谓词)内联目标? [英] How to inline a goal with findall/3, (use just one predicate)?
问题描述
我有一个看起来像这样的知识库
I have a knowledgebase that looks something like this
fact1(1, _, a, _, _).
fact1(2, _, c, _, _).
fact1(3, _, d, _, _).
fact1(4, _, f, _, _).
fact2(_, 1, b, _, _).
fact2(_, 2, c, _, _).
fact2(_, 4, e, _, _).
对于每个fact1
& fact2
,其中(在此示例中)数字匹配,我希望有一个对应的字母列表作为元组.
我想为此使用findall/3
和一个谓词.
For every fact1
& fact2
, where (in this example) the numbers match up, I want to have a list of the corresponding letters as tuples.
I would like to use findall/3
and only one predicate for this.
在此之前,我曾问过一个问题,该问题是如何解决类似问题的,答案是使用两个谓词.该解决方案如下所示:
I have asked a question here before on how to solve something similar, where the answer was using two predicates. That solution looked like this:
find_item((Val1,Val2)):-
fact1(A, _, Val1, _, _),
fact2(_, A, Val2, _, _).`
test(Items) :-
findall(Item,find_item(Item),Items).
给定事实示例的结果应如下所示:
The result for the given example of facts, should look like this:
[(a, b), (c, c), (f, e)]
是否可以仅使用 findall/3 ?
推荐答案
You can inline procedure find_item/1
as the goal of findall/3
(use a conjunction of goals instead of a single goal):
test(Items):-
findall((Val1, Val2), (fact1(A, _, Val1, _, _), fact2(_, A, Val2, _, _)), Items).
这篇关于如何使用findall/3(仅使用一个谓词)内联目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!