I'm trying make a program that can find the longest word that you can make with a given set of letters. (Kind of like Watson in Jeopardy)
So far I managed to make it so that if you specify the length of the word, it will give you all the words of that length you can make with the letters you gave it.
The program contains facts word/1
for all English words taken into consideration, like so:
word(ant).
word(computer).
% ... (and many more)
First, I made a predicate called word_letters/2
that turns a word into a list of letters, like the atom_chars/2
predicate.
Then, I wrote a cover/2
predicate to see if a list contains all letters of a previous list:
cover([],[]).
cover([],_).
cover([Head1|Tail1],List2) :-
member(Head1,List2),
select(Head1,List2,Newlist),
cover(Tail1,Newlist).
Then I wrote a solution/3
predicate that turns all the words of a certain length into a list of letters, and checks if that list is covered by letters you gave it first:
solution(Letters,Word,Length) :-
word(Word),
word_letters(Word,Letters2),
length(Letters2,Length),
word_letters(Word,Letters2),
cover(Letters2,Letters).
What I'm trying to do now is making it so that you don't have to specify the length of the word. It should just give the longest possible word, and tell you how long it is. It should work like this:
?- topsolution([g,i,g,c,n,o,a,s,t], Word, Score).
Word = agnostic,
Score = 8
True
It doesn't look that hard, but I just can't seem to make it work. It would be great if someone could help or maybe point me in the right direction!