更新一个列表元素的列表清单? [英] Update a list of a list of elements in a single list?
本文介绍了更新一个列表元素的列表清单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些code,其目的是在一个列表来替换值
I have some code which is designed to replace a value in a list
replaceNth n newVal (x:xs)
| n == 0 = newVal:xs
| otherwise = x:replaceNth (n-1) newVal xs
例如,当我加载功能到了ghci,我进入并得到如下:
For example, when I load the function into GHCI, I enter and get the following:
*Main> replaceNth 3 4 [3,3,3,3,3]
[3,3,3,4,3]
不过我想在一个列表中使用此功能的多个列表似乎并不能这样做(例如)。
However I am trying to use this function for a multiple lists within a list and can't seem to do so (e.g.).
我想是要得到这样的结果:
What I want is to get a result like this:
[[3,3,3,3,3],[3,3,3,**2**,3],[3,3,3,3,3]]
从这个[3,3,3,3,3],[3,3,3,3,3],[3,3,3,3,3]
From this [[3,3,3,3,3],[3,3,3,3,3],[3,3,3,3,3]]
推荐答案
这也许你想要做什么(适用于列表的列表):
perhaps this does what you want (applied to the list of lists):
replaceNth 1(replaceNth 3 4 [3,3,3,3,3])
这篇关于更新一个列表元素的列表清单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文