
[][H | T][john | [mary | [pat | []]]] is [john, mary, pat]member(X,[X|_]). member(X,[_|T]) :- member(X,T).
member(mary, [john, mary, pat]). true ; false member(ann, [john, mary, pat]). false ; member(X, [john, mary, pat]). X = john ; X = mary ; X = pat ; false.
append(X, Y, Z) means “It is true that Z is X appended with Y”append([], Ys, Ys). append([X | Xs], Ys, [X | Zs]) :- append(Xs, Ys, Zs).
append([john], [mary, pat], Z). Z = [john, mary, pat]. append(X, Y, [john, mary, pat]). X = [], Y = [john, mary, pat] ; X = [john], Y = [mary, pat] ; X = [john, mary], Y = [pat] ; X = [john, mary, pat], Y = [] ; false.
> >= < =< =:= =\=
= is unification (see next lecture), not “evaluate and assign”. The query X = 3 + 4. (or 3 + 4 = X.) yields X = 3 + 4. How helpful!is operator to force arithmetic evaluation.
X is 3 + 4. X = 7
is can contain variables, but they must already be bound to numbers7 = 3 + X. false
factorial(N, F) means F equals N!
factorial(0, 1). factorial(N, F) :- N > 0, N1 is N - 1, factorial(N1, F1), F is N * F1.
Note that factorial(N - 1, F1) would not work. (See the lab).
is to Evaluate ArithmeticN + 1 is internally represented as add(N, 1). numlist(A, B, L) yields the list of numbers [A, A+1, ..., B]numlist:
numlist(A, B, []) :- A > B. numlist(A, B, [A|Xs]) :- A =< B, numlist(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).
append(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).

move(X,Y)win(X) :- move(X,Y), not(win(Y)).
Pile of pebbles. Each player can take either one or two pebbles. A player wins if only one pebble is left after their turn.
move([o,o|T], [o|T]). move([o,o,o|T], [o|T]). win(X) :- move(X,Y), not(win(Y)).
[o] a winning position? No—there is no move([o],_).[o,o] a winning position? Yes. Take one pebble, and the other player loses.[o,o,o] a winning position? Yes. Take two pebbles, and the other player loses. [o,o,o,o] a winning position? No. If you take one pebble, the other player takes two. If you take two pebbles, the other player takes one.[o,o,o,o,o] a winning position? Yes. If you take one pebble, the other player has a losing position. Same with[o,o,o,o,o,o]. (Take two)[o,o,o,o,o,o,o] is a losing position—if you take one or two, the other player can win.
pow2(0, 1). pow2(N, F) :- N > 0, pow2(N-1, F1), F is 2 * F1.
Try it out. What is the result of pow2(3,X)?
3-1-1-1 > 0
Does it succeed? Does it fail? If so, why? What happens next?
3-1-1-1 > 0.
at the Prolog interpreter. What result do you get?
pow2 by forcing evaluation of the arithmetic. What is your code for pow2 now? Confirm that pow2(3, X) works.numlist(1, 8 L)?solvepow2(N,M) :- numlist(1, M, L), member(N, L), pow2(N, M).
Try out solvepow2(X,8). Explain why it works.
o functors, such as [o,o,o,o,o]. Your task is to write a predicate move(From, To) that checks whether one can legally move from game state From to game state To. For example,
move([o,o,o,o,o],Y). Y = [o,o,o] ; Y = [o,o,o,o] ; fail
Hint: It is very easy to check whether To is at least one less than F:
append(To, [o|_], From)
It is also easy to check that To is at least half as long as From. Append it to itself and check that you can append more to get From.
What is your move predicate?
win(X) :- move(X,Y), not(win(Y)).
For example,
win([o,o,o,o,o]). true ; fail.
Try out marble lists of length 1 to 10. Which ones are winning positions?