运行由其他函数产生的所有可能性的函数 [英] run function with all possibilities resulted from other function
问题描述
我有两个谓词:
foo(Y,X)
bar(Y,Z)
在运行 foo 之后,我怎样才能用 Y 的所有可能性运行 bar ?
After running foo, How can I run bar with all possibilities of Y ?
示例:
foo(Y, key) % all possibilities of Y => chat
% faq
% about
% search
我怎样才能在所有这些可能性下运行 bar?
How can I run bar with these all possibilities ?
bar(chat, Z)
bar(faq, Z)
bar(about, Z)
bar(serach, Z)
然后将Z
的所有结果存入一个列表Zs
?
And then store all the results of Z
in a list Zs
?
推荐答案
allZs(X, Zs) :-
setof(Y, foo(Y, X), Ys),
maplist(bar, Ys, Zs).
相关的 SWI-Prolog 手册页:寻找目标的所有解决方案 和 图书馆申请
related SWI-Prolog man pages: Finding all Solutions to a Goal and library apply
注意:通常在 Prolog 中,约定是将输入参数放在输出参数之前 - 在你的第一个谓词中,这意味着 foo(X, Y)
而不是 foo(Y, X)
.加上这里它会概述传递性:foo(X, Y), bar(Y, Z).
.
Note: usually in Prolog the convention is to put intput arguments before output ones - in your first predicate that'd mean foo(X, Y)
instead of foo(Y, X)
. Plus here it'd outline the transitivity: foo(X, Y), bar(Y, Z).
.
这篇关于运行由其他函数产生的所有可能性的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!