How do I make this predicate work? extractedKnowledge(SourceList,ExtractedList)
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 uninst开发者_开发问答antiated 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),
...?
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
精彩评论