【问题标题】:Prolog: Get all list elements recursivelyProlog:递归获取所有列表元素
【发布时间】:2017-10-22 13:20:22
【问题描述】:

这里我有一个模块规则列表。

module(oop). % Object Oriented Programming
module(se1). % Software Engineering 1
module(se2). % Software Engineering 2
% etc.

然后我定义了每个模块的先决条件。

require(oop, []). % oop is base module. It doesn't require any other modules.
require(se1, [oop]). % se1 requires oop
require(se2, [se1]). % se2 requires se1

如您所见,se2 需要 se1(直接)和 oop(间接)。 我想定义一个递归遍历列表并将所有需要的模块作为列表返回的函数。

查询:

?- dependent(se2, L).

预期结果:

L = [se1, oop]

如何在 Prolog 中使用递归编写依赖谓词?

非常感谢您的帮助。

【问题讨论】:

  • How can write the dependent functionProlog没有函数只有谓词...

标签: list recursion prolog


【解决方案1】:

一种使用相互递归的简单明了的方法:

dependent(X,L):- module(X), require(X,L1), find(L1,L).

find([],[]).
find([H|T],L):-dependent(H,L2),find(T,L3),append([H|L2],L3,L).

例子:

?- dependent(se2,L).
L = [se1, oop].

?- dependent(se1,L).
L = [oop].

?- dependent(oop,L).
L = [].

【讨论】:

    猜你喜欢
    • 2018-11-05
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多