Have to find the longest word possible out of a list of given letters PROLOG -


:-consult(words.pl). % words big database of                       % 30.000 used words in english language  topsolution([], _, p) :- %basecase, in case given list of letters                          %empty, output no word , give amount of letters                          %and call p     word(x),             %sees if x word     p = 0. topsolution(wordlist, x, p) :- %makes longest word can out of list                                 %of letters , outputs said word x, ,                                 %amount of letters has p     y = 0,                             solution(wordlist, x, y), %determines words can make given                               %list of letters , given length of said word     y1 y + 1,                  solution(wordlist, x, y1), %determines longest word of y + 1      wordlength(p, x).          %defines how many letters word x has , calls amount p 

so piece of code made find word. problem i'm struggling can't find way make recursion stop. if input:

?- topsolution([g,i,g], word, y).  

prolog outputs this:

false 

even though should output:

word = gig  y = 3 true 

i know why this. because y keep increasing 1, until reaches y = 4. since there no possible words 4 letters out of list consisting of 3 letters. fails.

so how guys recommend fixing this? how tell prolog should stop if encounters case cannot output word?

you should extremely suspicious of base case because of singleton value warning. it's important treat singletons in prolog errors, because represent misunderstanding between , prolog.

i think base case here wrong. when prolog fails make unification, output false; what's supposed happen. if call topsolution([g,g,g,g], word, p), should output false rather saying p = 0 , nothing word. there no solution. p = 0 "i found solution of length 0 i'm not telling is." better "i didn't find solution."

i think have 2 cases:

  1. i found word based on permutation of letters have right here
  2. i found word trying #1 on subset of letters have right here

your base case #1: have in hand permutation of letters of word, here's word:

topsolution(letters, word, p) :-     permutation(letters, wordletters),  % permute letters     atom_chars(word, wordletters),      % make permuted atom     word(word),                         % guard: it's word     length(letters, p).                 % what's length? 

then inductive case strip out letter , try again:

topsolution(letters, word, p) :-     select(_, letters, remainingletters),    % remove letter     topsolution(remainingletters, word, p).  % try again 

the recursion stop when you've entered second predicate body after exhausting permutations, every letter in sequence. select(_, [], _) false. there no p = 0 case worry here.


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -