给出一个字母列表,我试图找到可以用这些字母制作的最长字。
include('words.pl').
word_letters(Word, Letters) :- atom_chars(Word, Letters).
cover([],_).
cover([Head1|Tail1], List2) :- select(Head1,List2, List3), cover(Tail1, List3).
solution(ListLetters, Word, Length) :- word(Word),
word_letters(Word,LettersWord),
length(LettersWord, Length),
cover(LettersWord,ListLetters).
topsolution(Letters, Word, LMax) :- solution(Letters, Word, LMax).
topsolution(Letters, Word, LMax) :- select(_, Letters, RemainingLetters),
topsolution(RemainingLetters, Word, LMax).
word_letters谓词转换字母列表中的给定单词。 封面谓词告知给定列表是否包含在第二个列表中。
?-cover([a,b,c], [b, a, a, c])
true
解决方案谓词为给定的字母列表找到可以用这些字母及其长度制作的所有单词。
我的问题在于topsolution谓词应该只返回最大长度的解决方案,但它的工作原理就像解决方案一样。
您能帮我理解如何制作顶级解决方案,以便输出最大长度的单词?
这是我的单词数据库:tinyurl.com/prolog-words
答案 0 :(得分:0)
我使用另一个谓词解决了我的问题。
topsolution(Letters, Word, Lmax) :- length(Letters, N1),
topsolutionCount(Letters, Word, Lmax, N1).
topsolutionCount(_, _,_,0).
topsolutionCount(Letters, Word, N, N) :- solution(Letters, Word, N).
topsolutionCount(Letters, Word,Lmax, N) :- N1 is N-1,
topsolutionCount(Letters, Word, Lmax, N1).