Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please use Prolog to implement following function: replace (E1, L1, E2, L2) this holds when list L1 is the same as L2, except that in
please use Prolog to implement following function:
replace (E1, L1, E2, L2) this holds when list L1 is the same as L2, except that in one place where L1 has the value E1, L2 has E2. Note that only one occurrence of E1 is replaced. This must work in any mode in which at least one of L1 or L2 is a proper list (that is, either or a list whose tail is a proper list). For example: replace (2 , [1 , 2 , 3 ,4] , 5 , X) should have only the solution X [1 , 5 , 3 , 4] replace (2, [1 , 2 , 3 , 2 , 1] , 5 , X) should backtrack over the solutions X = [ 1 , 5 2 , 1] and X - [1,2,3,5,1]. replace (2 , X , 5 , [1 , 5 , 3 , 5 , 1] ) should backtrack over the solutions X = [1 , 2 , 3 , 5 , 1] and X = [1,5,3,2,1] replace (X , [a , b , c , d] , Y , [a , e , c , d] ) should have only the solution X = b , Y = e. replace (X, [1,2,3,2,1],Y, [1,5,3,5,1]) should have no solutions (it should fail) replace (E1, L1, E2, L2) this holds when list L1 is the same as L2, except that in one place where L1 has the value E1, L2 has E2. Note that only one occurrence of E1 is replaced. This must work in any mode in which at least one of L1 or L2 is a proper list (that is, either or a list whose tail is a proper list). For example: replace (2 , [1 , 2 , 3 ,4] , 5 , X) should have only the solution X [1 , 5 , 3 , 4] replace (2, [1 , 2 , 3 , 2 , 1] , 5 , X) should backtrack over the solutions X = [ 1 , 5 2 , 1] and X - [1,2,3,5,1]. replace (2 , X , 5 , [1 , 5 , 3 , 5 , 1] ) should backtrack over the solutions X = [1 , 2 , 3 , 5 , 1] and X = [1,5,3,2,1] replace (X , [a , b , c , d] , Y , [a , e , c , d] ) should have only the solution X = b , Y = e. replace (X, [1,2,3,2,1],Y, [1,5,3,5,1]) should have no solutions (it should 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