prolog replacing elements in list

青春壹個敷衍的年華 提交于 2019-12-20 05:34:08

问题


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]) )
   ).

回答1:


Looks like you should use Association lists.

see SWI-Prolog manual

and the online doc



来源:https://stackoverflow.com/questions/59014014/prolog-replacing-elements-in-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!