% This file contains predicates that will prove helpful for Prolog
% assignment #4.  It does not contain a definition for the 4-argument
% form of the anagram predicate.  It also does not contain a definition
% for the dictionary predicate, which should be loaded separately.

% succeeds iff arg1 is a member of arg2 (a list)
member(X, [X|_]).
member(X, [_|Rest]) :- member(X, Rest).

% append/2(L1, L2, L3) succeeds when L3 is the list formed by appending L1 to
% L2 (i.e., it contains all values from L1 in order followed by all values
% from L2 in order).
append([], L, L).
append([X|L1], L2, R) :- R = [X|L3], append(L1, L2, L3).

% remove/2(X, L1, L2) sets L2 to the list obtained by removing the first
% occurrence of instantiated value X from instantiated list L1; fails if
% X does not occur in L1
remove(X, [X|Rest], Rest).
remove(X, [Y|Rest], [Y|Rest2]) :- X \== Y, remove(X, Rest, Rest2).

% readint(P, N) takes an instantiated prompt in P and prompts the user for
% a value until a valid integer is entered.
readint(P, N) :- repeat, write(P), read(N), integer(N), !.

% anagram with no arguments prompts the user for a phrase and a maximum
% number of words to include in the answer, then prints all valid answers
% given the dictionary returned by the dictionary predicate
anagram :- write('Phrase to scramble (as a list of atoms)? '), read(L),
	readint('Max words in result? ', N), dictionary(D),
	anagram(L, N, D, R), write(R), nl, fail.
