首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >替换列表列表中的元素

替换列表列表中的元素
EN

Stack Overflow用户
提问于 2014-11-03 17:38:18
回答 1查看 3.5K关注 0票数 1

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

代码语言:javascript
复制
%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).

谓词在常规列表上工作得很好,但是我想让它在列表上工作,而且我还停留在一个小步骤上。

代码语言:javascript
复制
subs([]).
subs([Head|Tail], Index) :-
        replace((Head), Index, 'r', Board2),
        printRow(Board2),
        subs(Tail).

原始列表:

代码语言:javascript
复制
[ [  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 ]
]

输出:

代码语言:javascript
复制
[ [  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次(每个子列表的大小),谓词现在应该输出以下(所需的)列表:

期望输出:

代码语言:javascript
复制
[ [  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 ]
]

问题在于如何实施这一对策。代码应该如下所示,但我遗漏了一些东西:

代码语言:javascript
复制
subs([]).
subs([Head|Tail], Index) :-
        replace((Head), Index, 'r', Board2),
        printRow(Board2),
        Index is Index + 5
        subs(Tail, Index).

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

代码语言:javascript
复制
0  1  2  3  4

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-03 20:15:25

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

从你的例子来看,你想要把一个列表作为一个二维数组来处理,并替换那个数组中的单个单元格。如果是这样的话,这是一种方法(可能不是最优的):

代码语言:javascript
复制
%
% 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
  .

另一种方法(可能更优):

代码语言:javascript
复制
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.
  .                                          %
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26719774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档