

% listAnagrams.
%    reads a word from the user then lists all anagrams
listAnagrams :- write('Enter a word followed by a period: '), read(Word), nl,
                name(Word,List), permute(List,Result),
                name(Anagram,Result), write(Anagram), nl,
                % now fail to force it to go back and
                %    generate another, different, anagram
                fail.

% permute a list by removing the front element,
%    permuting the tail, and adding the element back in somewhere
permute([], []).
permute([Elem|Tail], Permutation) :-
   permute(Tail,PermTail), add(Elem, PermTail, Permutation).
 
% add an element to a list
%   base case: make it the front element
add(Elem, List, [Elem|List]).
%   general case: put it somewhere inside instead
add(Elem, [Head|Tail], [Head|AddedToTail]) :- 
   add(Elem, Tail, AddedToTail).

