selectwritelnis to Evaluate Arithmeticappend(X, Y, [john, mary, pat]).
factorial(3, X) => X = 6 factorial(X, 6) => fail
solvefac(N,M) :- numlist(1, M, L), member(N, L), factorial(N, M).
numlist(A, B, L) yields the list of numbers [A, A+1, ...,
B]solvefac(N,M) :- numlist(1, M, L), member(N, L), factorial(N, M).
selectselect(X, A, R) picks an element X from
A and sets R to the remainderselect(X, A, R) is true if
X is a member of A and R is the
rest, that is,
select(X, A, R) :- append(U, [X | V], A), append(U, V, R).
perm([], []). perm(A, B) :- select(X, A, R), perm(R, S), B = [X | S].
perm([1,2,3],X). X = [1, 2, 3] ; X = [1, 3, 2] ; X = [2, 1, 3] ; X = [2, 3, 1] ; X = [3, 1, 2] ; X = [3, 2, 1] ; fail
perm(A, [X|Xs]) :- select(X, A, R), perm(R, Xs).
writelnwriteln(X) prints X and succeeds.perm was
perm([], []). perm(A, B) :- select(X, A, R), select(X, B, R).
perm([1,2,3],X). X = [1, 2, 3] ; X = [2, 1, 3] ; X = [2, 3, 1] ; X = [2, 1, 3] ; X = [1, 2, 3] ; X = [1, 3, 2] ; X = [3, 1, 2] ; X = [1, 3, 2] ; X = [1, 2, 3] ; fail.
writeln to find out why:
perm(A, B) :- select(X, A, R), writeln(R), select(X, B, R).
is to Evaluate ArithmeticN + 1 is internally represented as add(N, 1).
numlist:
numlist(A, B, []) :- A > B. numlist(A, B, [A|Xs]) :- A =< B, numlst(A + 1, B, Xs).
numlist(1, 10, X). X = [1, 1+1, 1+1+1, 1+1+1+1, ...]
A + 1 with is
numlist(A, B, [A|Xs]) :- A =< B, A1 is A + 1, numlist(A1, B, Xs).
It is very easy to check whether
Tat least one less thanF
move(F, T) :- append(T, [o], S), append(S, _, F).
T is one o less than
SF is even longer- move([o,o,o,o,o],Y). Y = [] ; Y = [o] ; Y = [o, o] ; Y = [o, o, o] ; Y = [o, o, o, o] ; Stack overflow
move(F, T) :- append(S, _, F), append(T, [o], S).
move(F, T) :- append(T, [o|_], F).
move.[4, 3, 2, 2, 3, 6, 6, 5, 6, 2][2, 2, 2, 3, 3, 4, 5, 6, 6, 6][[1, 0], [2, 3], [3,
2], [4, 1], [5, 1], [6, 3]][0, 3, 2, 1, 1, 3]append, select, delete,
reverse, flatten, sumlist,
max_list, etc. findall, bagof,
setof
The Thirty-one Game. (Geoffrey Mott-Smith (1954)) From a deck of cards, take the Ace, 2, 3, 4, 5, and 6 of each suit. These 24 cards are laid out face up on a table. The players alternate turning over cards and the sum of the turned over cards is computed as play progresses. Each Ace counts as one. The player who first makes the sum go above 31 loses.
Describe three different representation for the game state.
[0,0,0,0,0,0]. After an ace and two threes are flipped over,
the state is [1,0,2,0,0,0].
We need to compute the value of a game state. If it is over 31, that state is losing.
In Java, this would simply be
for (int i = 0; i < state.length; i++) value += (i + 1) * state[i];
But in Prolog, we don't have loops, so we need to have a recursive helper.
value(L, N) :- value(L, 1, N).
(You can overload a function name if it has different arity.) Implement the helper function:
value([], _, 0). value([X|Xs], S, N) :- P is X * S, ..., ..., N is + P + ...
What is your implementation?
[1,0,2,0,0,0]?flip(0, 1). flip(1, 2). flip(2, 3). flip(3, 4).
Now we want to express game state changes that involve legal flips, from
[A X B] to [A Y B], where A and
B are sublists and flip(X, Y) is true. Of course,
that is sloppy notation which won't work in Prolog. How do you write that
F is a list that starts with a list A, then has a
single element X and is followed by a list B?
(You need to use append.)
flipmove(F, T), to indicate that it is
possible to change the state F to the state T by
flipping.
flipmove(F, T) :- append(...), flip(X, Y), append(...).
[0, 1, 2, 3, 0,
0]?move(F, T) to
indicate that it is legal to move from F to T,
that is, the move is possible, and the resulting value is < 32. What is
your definition of move(F, T)?[3, 2, 3, 3, 0,
0]?win(X) :- move(X,Y), not(win(Y)).
What happens when you run
win([0,0,0,0,0,0]).
You may have to wait a while for the answer.
next(X, Y) indicating that it is legal to move
from X to Y, and Y doesn't make the
other player win. (It is really easy.)[0,0,0,0,0,0]?