Due Date:
February 19, 2018 23:59 hours
Pick 6 of the 9 predicates and implement them in Prolog. Add code comments to show your understanding and demonstrate with test cases.
REQUIREMENTS:
- Find the last element of the list. last([a,b,c,d]),
- Find the second last element of a list. nextlast([a,b,c,d]),
- Find the K'th element of a list. kelement([a,b,c,d,e,f,g],5),
- Find out whether a list is a palindrome. palin([a,c,c,b,a]), palin([a,b,c,c,b,a]),
- Flatten a nested list structure. flatten([a,[b,[c,d],e]]),
- Eliminate consecutive duplicates of list elements. compress([a,a,a,a,b,c,c,a,a,d,e,e,e,e,f]),
- Drop every N'th element from a list. drop([a,b,c,d,e,f,g,h,i,k],3),
- Remove the K'th element from a list. remove([a,b,c,d],3),
- Insert an element at a given position into a list. insert(e,[a,b,c,d],3).
You may run a single unified test case such as "s."
s :-
last([a,b,c,d]),
nextlast([a,b,c,d]),
kelement([a,b,c,d,e,f,g],5),
numelements([a,b,c,d,e,f,g]),
reverse([a,b,c,d,e,f,g]).
Also you may use write & new line statements inside your predicates to show the output:
... , write('4. Number of elements: '),write(I),nl.
Submit a working .pl file that demonstrates the cases above with a significant number of code comments to explain the operation of each predicate and also provide test cases. Make sure you eliminate all singleton variable warnings.
Last Revised: February 19, 2018