【问题标题】:Replacing elements in list of lists PROLOG替换列表 PROLOG 列表中的元素
【发布时间】:2014-11-04 04:15:25
【问题描述】:

我开发了一个谓词,它用Value 替换列表List 的索引Index 的值,并创建一个新的更新列表NewList

%replace(List,Index,Value,NewList)

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

谓词在常规列表上工作正常,但我想让它在列表列表上工作,我有点卡住了。

subs([]).
subs([Head|Tail], Index) :-
        replace((Head), Index, 'r', Board2),
        printRow(Board2),
        subs(Tail).

原名单:

[ [  0 ,  1 ,  2 ,  3 ,  4 ] ,
  [  5 ,  6 ,  7 ,  8 ,  9 ] ,
  [ 10 , 11 , 12 , 13 , 14 ] ,  
  [ 15 , 16 , 17 , 18 , 19 ] ,  
  [ 20 , 21 , 22 , 23 , 23 ]
]

输出:

[ [  0 , r ,  2 ,  3 ,  4 ] ,  
  [  5 , r ,  7 ,  8 ,  9 ] ,  
  [ 10 , r , 12 , 13 , 14 ] ,  
  [ 15 , r , 17 , 18 , 19 ] ,  
  [ 20 , r , 22 , 23 , 23 ]
]

发生这种情况的原因很明显,因为它将每个子列表上的值替换为Index = 1。 为了解决这个问题,我考虑过实现一个计数器。通过每次迭代将索引增加 5(每个子列表的大小),谓词现在应该输出以下(所需的)列表:

所需的输出:

[ [  0 ,  r ,  2 ,  3 ,  4 ] ,
  [  5 ,  6 ,  7 ,  8 ,  9 ] ,
  [ 10 , 11 , 12 , 13 , 14 ] ,  
  [ 15 , 16 , 17 , 18 , 19 ] ,
  [ 20 , 21 , 22 , 23 , 23 ]
]

问题在于如何实现这个计数器。代码应如下所示,但我遗漏了一些内容:

subs([]).
subs([Head|Tail], Index) :-
        replace((Head), Index, 'r', Board2),
        printRow(Board2),
        Index is Index + 5
        subs(Tail, Index).

输出: subs(<Original List>, 7).

0  1  2  3  4

谁能给我一些关于如何实现它的帮助?

【问题讨论】:

  • 我有点困惑。您究竟希望subs 在列表列表中做什么?你现在有两个subs 子句。一个接受一个参数 (subs([]).),另一个接受两个参数 (subs([Head|Tail], Index) :-...)。当L 是一个列表时,请准确解释subs(L, Index) 的含义。
  • 它应该替换列表列表的Index = 'Index'的元素。虽然它会替换董事会的每个子列表。

标签: prolog


【解决方案1】:

你的问题陈述有点不清楚。

从您的示例看来,您希望将列表列表视为本质上的二维数组,并替换该数组中的单个单元格。如果是这样,这是一种方法(可能不是最佳的):

%
% replace a single cell in a list-of-lists
% - the source list-of-lists is L
% - The cell to be replaced is indicated with a row offset (X)
%   and a column offset within the row (Y)
% - The replacement value is Z
% - the transformed list-of-lists (result) is R
%
replace( L , X , Y , Z , R ) :-
  append(RowPfx,[Row|RowSfx],L),     % decompose the list-of-lists into a prefix, a list and a suffix
  length(RowPfx,X) ,                 % check the prefix length: do we have the desired list?
  append(ColPfx,[_|ColSfx],Row) ,    % decompose that row into a prefix, a column and a suffix
  length(ColPfx,Y) ,                 % check the prefix length: do we have the desired column?
  append(ColPfx,[Z|ColSfx],RowNew) , % if so, replace the column with its new value
  append(RowPfx,[RowNew|RowSfx],R)   % and assemble the transformed list-of-lists
  .

另一种方式(可能更优化):

replace( [L|Ls] , 0 , Y , Z , [R|Ls] ) :- % once we find the desired row,
  replace_column(L,Y,Z,R)                 % - we replace specified column, and we're done.
  .                                       %
replace( [L|Ls] , X , Y , Z , [L|Rs] ) :- % if we haven't found the desired row yet
  X > 0 ,                                 % - and the row offset is positive,
  X1 is X-1 ,                             % - we decrement the row offset
  replace( Ls , X1 , Y , Z , Rs )         % - and recurse down
  .                                       %

replace_column( [_|Cs] , 0 , Z , [Z|Cs] ) .  % once we find the specified offset, just make the substitution and finish up.
replace_column( [C|Cs] , Y , Z , [C|Rs] ) :- % otherwise,
  Y > 0 ,                                    % - assuming that the column offset is positive,
  Y1 is Y-1 ,                                % - we decrement it
  replace_column( Cs , Y1 , Z , Rs )         % - and recurse down.
  .                                          %

【讨论】:

  • 我很抱歉在我的陈述中可能存在误导,但它已经进行了将近 8 小时的核心 Prolog'ing。是的,这正是我正在寻找的。我构建的谓词(替换)适用于列表,但不适用于二维列表。对于 subs,我试图单独分析每个子列表,并在每次迭代后增加索引,这样它就不会回到 0。我离得太远了吗?稍后我将编辑我的问题,以便更多人能够理解我想说的话。
猜你喜欢
  • 1970-01-01
  • 2011-08-16
  • 2013-03-21
  • 2016-03-28
  • 2018-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多