Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

likes(mary,food). likes(mary,wine). likes(john,wine). likes(john,mary). What do

ID: 3556270 • Letter: L

Question

likes(mary,food).

likes(mary,wine).

likes(john,wine).

likes(john,mary).

What do the following queries yield?

| ?- likes(mary,food).

| ?- likes(john,wine).

| ?- likes(john,food).

How do you add the following facts?

p([ ]).

p([A]).

p([A, B |T]) :- A > B, p(T).

indicate each of the goals below which will succeed when given to the interpreter:

p([1,2,3]).

p([3,2,1]).

p([10,5]).

p([8]).

p([4,2,1,6,3]).

Mammals have four legs and no arms, or two arms and two legs. A cow has no arms.

Can Prolog derive the conclusion that a cow has four legs? How do you improve your answer so that it rules out cows with 2 arms?

Explanation / Answer

Write a Prolog procedure to compute the sum of whole numbers from 1 to N.

sum(N, Sum) :-
Sum is (N + 1) * N / 2 .

Define a predicate brother(X,Y) which holds iff X and Y are brothers.

brother(X,Y) :- parent(A,X),parent(A,Y),male(Y).

2-Define a predicate cousin(X,Y) which holds iff X and Y are cousins.
sol-
cousin(X,Y) :- father(Z,X), father(W,Y), brother(Z,W).
3 Define a predicate grandson(X,Y) which holds iff X is a grandson of Y.
sol-
grandson(X,Y) :- father(Z,X), father(Y,Z).
4 Define a predicate descendent(X,Y) which holds iff X is a descendent of Y.
sol-
descendent(X,Y) :- father(Y,X).   
descendent(X,Y) :- father(Z,X),            descendent(Z,Y).
5 Draw the genealogical tree.

a
/
b c
/
d e f   

1 Define a predicate length(L,N) which holds iff N is the length of the list L.
sol-
length([],0).
length([_|L],N) :- length(L,M), N is M+1.
1 Define a predicate sumlist(L,N) which, given a list of integers L, returns the sum N of all the elements of L.
sol-
sumlist([],0).
sumlist([X|L],N) :- sumlist(L,M), N is M+X.