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

Prolog Please complete the following problems. Your response should include the

ID: 3761370 • Letter: P

Question

Prolog

Please complete the following problems. Your response should include the prolog files with a pdf file with the code for the problem as well as the results of executing the included test cases. If you copy any code from the Internet, please list the site from where you borrowed the code Problem: Write a predicate (first_middle_last/4) to find the first, middle, and last elements of a list. Some built-in predicates that may help you: nth0/3 (this is written as nth/3 in the above link, but t should actually be nth0/3), length/2, div/2 (performs integer division). You may use any built-in predicates you like. There is a section on List Processing that you might find helpful. After implementing your predicate, please run the following tests and include the answers in your pdf submission (screenshots might work best). a. first_middle_last ([1,2,3], First, Middle, Last). b. first_middle_last ([1,2,3,4,5], First, Middle, Last). c. first_middle_last ([19,25,72,9,4,15,23,19,32,41,53], First, Middle, Last).

Explanation / Answer

list_first_middle_last([X|Xs],X,M,L) :- fwList_list_middle_last([X|Xs],[X|Xs],M,L). fwList_list_middle_last([], [E| _],E,E). fwList_list_middle_last([L], [E| _],E,L). fwList_list_middle_last([_,L], [E| _],E,L). fwList_list_middle_last([_,_,X|Xs],[_|Es],E,L) :- fwList_list_middle_last([X|Xs],Es,E,L). ?- length(Xs,N), numlist(1,N,Xs), list_first_middle_last(Xs,X0,Xm,Xz). N = 1, Xs = [1], X0 = 1, Xm = 1, Xz = 1 ; N = 2, Xs = [1,2], X0 = 1, Xm = 1, Xz = 2 ; N = 3, Xs = [1,2,3], X0 = 1, Xm = 2, Xz = 3 ; N = 4, Xs = [1,2,3,4], X0 = 1, Xm = 2, Xz = 4 ; N = 5, Xs = [1,2,3,4,5], X0 = 1, Xm = 3, Xz = 5 ; N = 6, Xs = [1,2,3,4,5,6], X0 = 1, Xm = 3, Xz = 6 ; N = 7, Xs = [1,2,3,4,5,6,7], X0 = 1, Xm = 4, Xz = 7 ; N = 8, Xs = [1,2,3,4,5,6,7,8], X0 = 1, Xm = 4, Xz = 8 ; N = 9, Xs = [1,2,3,4,5,6,7,8,9], X0 = 1, Xm = 5, Xz = 9 ...