List 第一次出现后插入Prolog元素

List 第一次出现后插入Prolog元素,list,prolog,meta-predicate,prolog-dif,List,Prolog,Meta Predicate,Prolog Dif,如何在列表Xs中第一次出现X之后添加元素E 例如: ?- insert_right_behind(5,10,[2,4,10,12],Xs). Xs = [2,4,10,5,12]. % expected answer 此刻,我在理解上有困难 由于我对该语言不熟悉,因此需要进行递归 提前谢谢 使用三个谓语从句: % Inserting after in an empty list is an empty list: insert_after( _X, _Y

如何在列表
Xs
中第一次出现
X
之后添加元素
E

例如:

?- insert_right_behind(5,10,[2,4,10,12],Xs).
Xs = [2,4,10,5,12].                     % expected answer
此刻,我在理解上有困难 由于我对该语言不熟悉,因此需要进行递归


提前谢谢

使用三个谓语从句:

% Inserting after in an empty list is an empty list:
insert_after( _X, _Y, [], [] ).

% If the "after" item is at the head of the list, then the "insert" item can go after it:
insert_after( X, Y, [Y|T], [Y,X|T] ).

% If the head of the list isn't the "after" item, then the result will be
% this with a new tail list that has the "insert" item inserted:
insert_after( X, Y, [H|T], [H|L] ) :-
    Y \= H,
    insert_after( X, Y, T, L ).

如果给定列表中不存在“after”项,则
insert\u after/4
将生成原始列表。通过删除上述子句之后的第一个insert\u,它将在这种情况下失败。

使用三个谓词子句:

% Inserting after in an empty list is an empty list:
insert_after( _X, _Y, [], [] ).

% If the "after" item is at the head of the list, then the "insert" item can go after it:
insert_after( X, Y, [Y|T], [Y,X|T] ).

% If the head of the list isn't the "after" item, then the result will be
% this with a new tail list that has the "insert" item inserted:
insert_after( X, Y, [H|T], [H|L] ) :-
    Y \= H,
    insert_after( X, Y, T, L ).

如果给定列表中不存在“after”项,则
insert\u after/4
将生成原始列表。通过删除上面的第一个
insert\u after
子句,在这种情况下它将失败。

让我们保持它的简单和易用性,如下所示:


让我们保持它的简单和使用,就像这样:

在最成功的查询中,会留下无用的选择点

我们可以通过这样使用来避免这些选择点:

让我们运行一些查询

?- item_following_in_inserted(5,10,[2,4,12],Xs).
false.                                               % OK, unchanged

?- item_following_in_inserted(5,10,[2,4,10,12],Xs).
Xs = [2,4,10,5,12].                                  % succeeds deterministically

?- item_following_in_inserted(5,10,[2,4,10,12,10],Xs).
Xs = [2,4,10,5,12,10].                               % succeeds deterministically

?- item_following_in_inserted(I,E,Xs,Ys).
  Xs = [      E|_Z], Ys = [      E,I|_Z]             % OK, unchanged
; Xs = [   _A,E|_Z], Ys = [   _A,E,I|_Z], dif(E,_A)
; Xs = [_A,_B,E|_Z], Ys = [_A,_B,E,I|_Z], dif(E,_A), dif(E,_B)
...
在最成功的查询中,会留下无用的选择点

我们可以通过这样使用来避免这些选择点:

让我们运行一些查询

?- item_following_in_inserted(5,10,[2,4,12],Xs).
false.                                               % OK, unchanged

?- item_following_in_inserted(5,10,[2,4,10,12],Xs).
Xs = [2,4,10,5,12].                                  % succeeds deterministically

?- item_following_in_inserted(5,10,[2,4,10,12,10],Xs).
Xs = [2,4,10,5,12,10].                               % succeeds deterministically

?- item_following_in_inserted(I,E,Xs,Ys).
  Xs = [      E|_Z], Ys = [      E,I|_Z]             % OK, unchanged
; Xs = [   _A,E|_Z], Ys = [   _A,E,I|_Z], dif(E,_A)
; Xs = [_A,_B,E|_Z], Ys = [_A,_B,E,I|_Z], dif(E,_A), dif(E,_B)
...

如果
10
不存在,您希望它做什么?mbratch,
false
很可能,false是正确的结果,那么我猜。mbrathc,“first”出现。插入只在遇到第一个元素后发生一次。如果
10
不存在,您希望它做什么?mbratch,
false
yes,false可能是正确的结果,然后我猜。mbrathc,“first”出现。插入只在遇到第一个元素后发生一次。谢谢,此解决方案按预期工作,并且很高兴您对您的答案提供了一些注释。谢谢,这个解决方案按照预期的方式工作,而且您对您的答案提供了一些评论也很好。 ?- item_following_in_inserted(5,10,[2,4,10,12,10],Xs). Xs = [2,4,10,5,12,10] % single solution ; false. % terminates universally ?- item_following_in_inserted(I,E,Xs,Ys). Xs = [ E|_Z], Ys = [ E,I|_Z] ; Xs = [ _A,E|_Z], Ys = [ _A,E,I|_Z], dif(E,_A) ; Xs = [ _A,_B,E|_Z], Ys = [ _A,_B,E,I|_Z], dif(E,_A), dif(E,_B) ; Xs = [_A,_B,_C,E|_Z], Ys = [_A,_B,_C,E,I|_Z], dif(E,_A), dif(E,_B), dif(E,_C) ...
item_following_in_inserted(I,J,[X|Xs],Ys0) :-
   if_(J = X,
       Ys0 = [J,I|Xs],
       (Ys0 = [X|Ys], item_following_in_inserted(I,J,Xs,Ys))).
?- item_following_in_inserted(5,10,[2,4,12],Xs).
false.                                               % OK, unchanged

?- item_following_in_inserted(5,10,[2,4,10,12],Xs).
Xs = [2,4,10,5,12].                                  % succeeds deterministically

?- item_following_in_inserted(5,10,[2,4,10,12,10],Xs).
Xs = [2,4,10,5,12,10].                               % succeeds deterministically

?- item_following_in_inserted(I,E,Xs,Ys).
  Xs = [      E|_Z], Ys = [      E,I|_Z]             % OK, unchanged
; Xs = [   _A,E|_Z], Ys = [   _A,E,I|_Z], dif(E,_A)
; Xs = [_A,_B,E|_Z], Ys = [_A,_B,E,I|_Z], dif(E,_A), dif(E,_B)
...