1

I need help with understanding how to work with lists like that [a(b,c),a(x,d)] change(S,K,R) changes first list [a,c,b] with values given in second list [c(a,x),c(b,y)]

?- change([a,c,b],[k(a,x),k(b,y)],R).
R = [x,c,y].

% my program but it works with second list that is of wrong list elements type k(a,x) but like [a,x] and works kinda poorly cause return True instead of R= x,c,y, if i print R value it is [y,c,x|_2826] i call my code with ?- change([a,c,b],[a,x,b,y],R).

change([],[],[]):-!.
change([],[],R):-write(R),!.
change([H1|T1],[],[H1|R]):-change(T1,[],R),!.
change([H1|T1],[H2,H3|T2],R) :-
   (  ( H1==H2 , change(T1,T2,[H3|R]) )
      ; ( H1\==H2, change(T1,[H2,H3|T2],[H1|R]) )
   ).
false
  • 10,264
  • 13
  • 101
  • 209
V.Cunichin
  • 27
  • 10

1 Answers1

0

Looks like you should use Association lists.

see SWI-Prolog manual

and the online doc

Anton Danilov
  • 1,246
  • 11
  • 26
  • I can't really understand from the manual, the only thing i found is this part of code assoc([K-V|_], K, V). assoc([_|AssocRest], Key, Value) :- assoc(AssocRest, Key, Value). i can't understand how to compare if key matches my element in first list – V.Cunichin Nov 24 '19 at 12:19
  • First look at the answer https://stackoverflow.com/questions/50069875/associative-lists-in-prolog ... Don't hesitate to ask for more – Anton Danilov Nov 24 '19 at 16:55
  • Have a look also there https://stackoverflow.com/questions/53989755/associative-list-in-prolog?noredirect=1&lq=1 – Anton Danilov Nov 25 '19 at 02:36