Question
/* Problem 5: Write a predicate element_at(X,Y,N) that succeeds if X is the Nth element of list Y. Y is 0-indexed. NOTE: Don't worry about
/* Problem 5: Write a predicate element_at(X,Y,N) that succeeds if X is the Nth element of list Y. Y is 0-indexed.
NOTE: Don't worry about the error cases: i.e, N greater than the length of Y. */
/* Problem 5 Test: */ %:- element_at(3,[1,2,3],2). % SUCCEED %:- element_at(1,[1,2,3],0). % SUCCEED
%:- element_at(1,[1,2,3],1) -> fail ; true. % FAIL
/* Problem 6: Write a predicate insert_at(E,Y,N,Z) that succeeds if Z is the list Y with E inserted at index N -- Insert X at index N in Y.
NOTE: Don't worry about the error cases: i.e, N greater than the length of Y. */
/* Problem 6 Test: */ %:- insert_at(3,[1,2,3],2,[1,2,3,3]). % SUCCEED %:- insert_at(1,[1,2,3],0,[1,1,2,3]). % SUCCEED %:- insert_at(a,[1,2,3],1,[1,a,2,3]). % SUCCEED
%:- insert_at(1,[1,2,3],0,[1,2,3]) -> fail ; true. % FAIL
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started