我如何使这个谓词发挥作用?提取的知识(源列表,提取列表)

发布于 2024-10-26 18:35:09 字数 461 浏览 5 评论 0原文

我有这样的断言:我似乎无法开始工作。 谓词应按以下方式使用:为谓词提供一个事物列表 (KnowledgeList) 和一个未实例化的变量 (ExtractedList)。 然后谓词应该继续填充 ExtractedList。 为了填充 ExtractedList,它迭代 KnowledgeList 的项目, 构建一个新的提取内容列表,并将该列表附加到 ExtractedList。

我知道我可能应该使用递归来解决这个问题,但我现在感到很困惑。

extractedKnowledge(KnowledgeList, ExtractedList) :-
  list(KnowledgeList),
  ExtractedList = [],
  length(KnowledgeList,ListLength),
  for(X,1,ListLength),
  nth(X,KnowledgeList,ListElement),
  ...?

I have this predicate that I can't seem to get to work.
The predicate should be used the following way: You give the predicate a list of things (KnowledgeList), and an uninstantiated variable (ExtractedList).
The predicate then should proceed filling ExtractedList.
To fill ExtractedList it iterates over the items of KnowledgeList,
builds a new list of extracted things, and appends this list to ExtractedList.

I know I should probably use recursion to solve this, but I feel stumped at the moment.

extractedKnowledge(KnowledgeList, ExtractedList) :-
  list(KnowledgeList),
  ExtractedList = [],
  length(KnowledgeList,ListLength),
  for(X,1,ListLength),
  nth(X,KnowledgeList,ListElement),
  ...?

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

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

发布评论

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

评论(1

停滞 2024-11-02 18:35:09

由于您将 ExtractedList 统一为 [],因此它将始终为空。你确实应该使用递归。这是一个可以帮助您入门的骨架递归程序:

% base case: we can only extract 0 items from 0 items
extracted_knowledge([],[]).
% recursive case
extracted_knowledge([Item|Knowledge], Extracted) :-
    extracted_knowledge(Knowledge, Extracted0),
    % build Extracted from Extracted0 by adding Item,
    % if it needs to be extracted

Since you unify ExtractedList which [], it will always be empty. You should indeed use recursion. Here's a skeletal recursive program to get you started:

% base case: we can only extract 0 items from 0 items
extracted_knowledge([],[]).
% recursive case
extracted_knowledge([Item|Knowledge], Extracted) :-
    extracted_knowledge(Knowledge, Extracted0),
    % build Extracted from Extracted0 by adding Item,
    % if it needs to be extracted
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文