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

GENERAL DIRECTIONS : Write, compile, and run four Prolog programs. Use SWI-Prolo

ID: 3573815 • Letter: G

Question

GENERAL DIRECTIONS: Write, compile, and run four Prolog programs. Use SWI-Prolog. Turn in the source code and the sample runs pasted onto the end of the source code.

1. Write a recursive Prolog predicate that finds the max value in a list of numbers. Your queries should look like maxList([3,5,2,1,7,4], N) where N will be instantiated to the largest number in L.

Test Case: maxList( [ 3,5,2,23,7,4 ,1,5 ], N).

2. Write a recursive Prolog predicate listRef(L,N,R) to find the Nth member of a the list L. The answer should be in R. If N > length of the list, the sytem shuld respond with false.

TestCases: listRef([a,b,c,d,e,f], 5,R) and listRef([cat, dog, bird], 4, R).

3. Write a recursive Prolog predicate to remove duplicates from a list. Your queries should look like removeDups( [1,3,5,2,5,7,1,9], X) where X will be instantiated to [ 3,2,5,7,1,9] and where order of the elements of X does not matter.

Testcases: removeDups( [ 1,3,2,3,2,1,4,5], X) and removeDups([q,w,e,r,ty] , X). Do NOT USE sort.

4. Write a Prolog program to solve the general Challenger puzzle below. Your predicate should be challenger(A1,A2,A3,A4,A5,A6,A7,A8,A9,A10, X1,X2,X3,X4,X5,X6,X7,X8,X9,X10,X11,X12,X13,X14,X15,X16) . To solve problem generate the Xs and check the equations in an efficient order.

Test Case: Solve the particular Challenger puzzle. To solve the particular puzzle, your query will be challenger(A1,A2,A3,A4,A5,A6,A7,A8,A9,A10, X1,X2,X3,X4,X5,X6,X7,X8,X9,X10,X11,X12,X13,X14,X15,X16) with values substituted for the As and the given Xs.

Specific challenger Problem 20 16 13 18 33 14 Format of the General Challenger Problem X4 X6 X10 X11 X12 X15 X13 X14 X16 A10 A9 A7 Note: The diagonal equations are X1 +X6 X11 X16 and X13 +X10+ X7 +X4 A1.

Explanation / Answer

***********************Program*******************************

maxlist([H|T],N):-
length(T,L),
L>0 ->
(
maxlist(T,N1),
(
H > R1 ->
N is H
;
N is N1
)
)
;
N is H.

************************input*******************************

maxlist([3,5,2,23,7,4 ,1,5 ],N).

*************************output*********************************

Success time: 0 memory: 76480 signal:0