Question: WRITE CODE IN PROLOG Define a relation my_replace that takes an association list ALIST and an arbitrary list L, that binds to a third argument

WRITE CODE IN PROLOG

Define a relation my_replace that takes an association list ALIST and an arbitrary list L, that binds to a third argument R the list L with each variable (key) in ALIST replaced with the corresponding value it is bound to in ALIST.

Tests:

my_replace([g,c,c,g,t,a,a,u],[g,a,t,c,c,t,c,c,a,t,a,t,a,c,a,a,c,g,g,t],R).

R=[c,u,a,g,g,a,g,g,u,a,u,a,u,g,u,u,g,c,c,a]

my_replace([ucb,ucla,ucsd,uci,basketball,tennis],[ucsd,is,playing,basketball,against,ucb],R).

R=[uci,is,playing,tennis,against,ucla]

my_replace([dog,cat,fleas,kittens,sunday,friday],[my,dog,has,fleas,on,sunday],R).

R=[my,cat,has,kittens,on,friday]

please provide explanations as well.

FYI: my-assoc is a function that already exists in the prog.

my_assoc(_, [], _):-fail. my_assoc(Key, [Key, Value|_], Value). my_assoc(Key, [_|T], Value) :- my_assoc(Key, T, Value).

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!