【问题标题】:Prolog - Replacing X number of elements of a list after element of order IProlog - 在顺序 I 的元素之后替换列表的 X 个元素
【发布时间】:2015-01-08 09:52:11
【问题描述】:

我正在尝试实现一个谓词,该谓词在给定索引之后(并包括)替换列表的 NumElm 元素。为此,我使用另一个谓词来替换列表的元素。 到目前为止我的代码是:

replace([_|T], 0, X, [X|T]).
replace([H|T], I, X, [H|R]):- 
  I > -1,
  NI is I-1,
  replace(T, NI, X, R), !.
replace(L, _, _, L).

replaceX(_,_,0,_,_).
replaceX(Line,Index,NumElm,Elm,NLine) :-
  replace(Line,Index,Elm,BLine),
  Index1 is Index+1,
  NumElm1 is NumElm-1,
  replaceX(BLine,Index1,NumElm1,Elm,Nline).

使用该代码,仅替换第一个元素,即 order Index 的元素。 有任何想法吗? 提前致谢。 [编辑] 前面代码的结果只是“是”。替换第一个元素的元素如下:

replace([_|T], 0, X, [X|T]).
replace([H|T], I, X, [H|R]):- 
  I > -1,
  NI is I-1,
  replace(T, NI, X, R), !.
replace(L, _, _, L).

replaceX(_,_,0,_,_).
replaceX(Line,Index,NumElm,Elm,NLine) :-
  replace(Line,Index,Elm,NLine),
  Index1 is Index+1,
  NumElm1 is NumElm-1,
  replaceX(NLine,Index1,NumElm1,Elm,Nline).

【问题讨论】:

    标签: list replace prolog elements


    【解决方案1】:

    我认为您需要将 replaceX 的第一个子句定义为:

    replaceX(A,_,0,_,A):-!.
    

    _ 的工作原理解释了这背后的原因:它命名了一个匿名变量,而 _ 的每个实例都命名了一个不同的变量。 Here's 解释。另一方面,剪切可以帮助您摆脱不希望的回溯。

    我认为最后一行的Nline 应该是NLine。此外,您在第二个 replace 子句的末尾缺少一个点。

    编辑:

    replace([_|T], 0, X, [X|T]).
    replace([H|T], I, X, [H|R]):- 
      I > -1,
      NI is I-1,
      replace(T, NI, X, R), !.
    replace(L, _, _, L).
    
    replaceX(A,_,0,_,A):- !.
    replaceX(Line,Index,NumElm,Elm,NLine) :-
      replace(Line,Index,Elm,BLine),
      Index1 is Index+1,
      NumElm1 is NumElm-1,
      replaceX(BLine,Index1,NumElm1,Elm,NLine).
    

    示例输入:

    ?- replaceX([0,1,2,3,4,5,6],2,4,a,R).
    R = [0,1,a,a,a,a,6].
    

    这不是预期的结果吗?

    【讨论】:

    • 您的意思是添加该条款还是用您的条款替换我的条款?无论哪种方式,输出都是肯定的。
    • 替换你的。我将编辑以包含代码。您使用的解释器是什么?我知道巴西的大多数大学都使用 SWI prolog,我对此进行了测试
    【解决方案2】:

    在 Prolog 中处理数字索引可能很乏味,我会写

    replace(L, From, To, X, R) :-
        findall(S, (nth0(I,L,E), (I>=From, I<To ->S = X ; S = E)), R).
    

    产生

    ?- replace([0,1,2,3,4,5,6], 2,4,a, R).
    R = [0, 1, a, a, 4, 5, 6].
    

    【讨论】:

    • 不幸的是,我还没有学会那个语法,所以我不确定我能不能使用它。谢谢,不过
    【解决方案3】:

    这是一种方法:

    replace( As     , _ , 0 , _ , As     ) .  % special case: nothing left to replace.
    replace( [A|As] , P , N , X , [A|Bs] ) :- % otherwise,
      P > 0 ,                                 % - if the position counter is greater than zero,
      P1 is P-1 ,                             % - decrement it, stow the current item to the result list
      replace( As , P1 , N , X , Bs )         % - and recurse down
      .                                       %
    replace( [_|As] , 0 , N , X , [X|Bs] ) :- % otherwise, if the position count is decremented to zero
      N > 0 ,                                 % - but the length counter is greater than zero,
      N1 is N-1 ,                             % - decrement it, stow X in the result list
      replace( As , 0 , N1 , X , Bs )         % - and recurse down.
      .                                       %
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-16
      • 1970-01-01
      • 2013-03-21
      • 2016-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-27
      相关资源
      最近更新 更多