%***************************************************************************** %Anagrams and Backtracking % %***************************************************************************** :- ensure_loaded('listprocessing'). :- ensure_loaded('utilities'). %===================== % Word Database %===================== word(nook). word(look). word(invent). word(code). word(logic). word(bedtime). word(runtime). word(run). word(like). word(destroy). word(geek). word(software). word(prolog). word(artificial). word(nonmonotonic). aFavorite(logic). aFavorite(geek). %------------------------------------------------------------------------------------- % BackTracking and the Cut. %------------------------------------------------------------------------------------- bting() :- word(X), aFavorite(X), nl, writenl(X), !, word(Y), writeseqnl([Y,' ']), fail. %------------------------------------------------------------------------------------- % Generate and Test % %------------------------------------------------------------------------------------- anagram(Problem,Solution) :- atom_chars(Problem,ProblemLetters), perm(ProblemLetters,Attempt), atom_chars(Solution,Attempt), word(Solution),!. %------------------------------------------------------------------------------------- % If word is more than a few letters number of permutations % is more than number of words in the dictionary. % So to Prune Search Start with known words then just % check if known word " is a permutation" of problem word. % % Compare the execution times of the two versions on a long word. %------------------------------------------------------------------------------------- anagram1(Problem,Solution) :- atom_chars(Problem,ProblemLetters), word(Solution), atom_chars(Solution,SolutionLetters), isPerm(ProblemLetters,SolutionLetters),!.