且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用序言替换两个列表之间的元素

更新时间:2022-12-11 08:23:27

关于

myReplace(E1,[E1|Xs],E2,[E2|Xs]).

myReplace(E1,[H|Hs],E2,[H|Ts]):-
  myReplace(E1,Hs,E2,Ts).

?

如果我没记错的话,这将强制进行一次(并且只有一次)替代.

If I'm not wrong, this impose one (and only one) replacement.

记入您必须删除的内容

myReplace(E1,[],E2,[]).

否则,您会得到L1 = L2(无替代品)作为解决方案.

otherwise you get L1 = L2 (no replacement) as a solution.

并观察到,正如Lurker所指出的,这不是"L1中E1的第一次出现被替换".

And observe that, as pointed by Lurker, this isn't "the very first occurrence of E1 in L1 gets replaced".