Question: 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:

please use Prolog to implement following function: replace (E1, L1, E2, L2)

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!