:-consult(words.pl). % words is basically a big database of the
% 30.000 most used words in the english language
topsolution([], _, P) :- %basecase, in case the given list of letters is
%empty, output no word and give the amount of letters
%and call it P
word(X), %sees if X is a word
P = 0.
topsolution(WordList, X, P) :- %Makes the longest word it can out of a list
%of letters and outputs said word as X, and the
%amount of letters it has as P
Y = 0,
solution(WordList, X, Y), %Determines what words you can make with a given
%list of letters and a given length of said word
Y1 is Y + 1,
solution(WordList, X, Y1), %Determines the longest word of Y + 1
wordLength(P, X). %Defines how many letters a word X has and calls that amount P
これは私がその単語を見つけるために作ったコードです。私が苦労している唯一の問題は、再帰を止める方法を見つけることができないということです。私はそれがこれを行う理由を知っている与えられた文字のリストから可能な限り長い単語を見つける必要があります。PROLOG
Word = gig
Y = 3
true
:現在、I入力場合:
?- topsolution([g,i,g], Word, Y).
Prologの出力この:
false
でも、それが出力するけれども。それは、YがY = 4に達するまで、Yが1ずつ増加し続けているからです。リストのうち3文字のみからなる4文字の可能な単語がないためです。明らかに失敗します。
これを修正することをお勧めしますか?単語を出力できない場合には、プロローグは基本的にどのように停止すべきだと伝えるのですか?
'solution/3'のコードがなければ、これをデバッグすることは非常に難しいでしょう。 –