替换列表 PROLOG 列表中的元素

编程入门 行业动态 更新时间:2024-10-27 20:26:23
本文介绍了替换列表 PROLOG 列表中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I've developed a predicate which replaces the value of the index Index of a list List with Value and creates a new updated list 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).

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

The predicate works fine on regular lists, but I want to make it work on a list of lists and I am kind of stuck on a little step.

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(每个子列表的大小),谓词现在应该输出以下(所需的)列表:

It is noticeable why this happens, since it replaces the value with Index = 1 on each sublist. In order to fix it, I thought about implementing a counter. By incrementing the index by 5 each iteration (size of each sub list), the predicate should now output the following (desired) list:

所需的输出:

[ [ 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 ] ]

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

And the issue lies on how to implement that very counter. The code should look like the following but there's something I'm missing out on:

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

输出: subs(<原始列表>, 7).

0 1 2 3 4

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

Can anyone give me some help on how to implement it?

推荐答案

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

Your problem statement is a bit unclear.

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

From your examples, it would appear that what you want to treat a list-of-lists as essentially a 2-D array, and replace a single cell within that array. If so, this is one way (probably non-optimal) to do that:

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

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

Another way (probably more optimal):

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

更多推荐

替换列表 PROLOG 列表中的元素

本文发布于:2023-11-10 15:41:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575777.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   列表   列表中   PROLOG

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!