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