arrays - 在 Haskell 中更改数组中的条目
问题描述
假设你有
let a = array ((1,1),(2,2)) [((2,1),3),((1,2),2),((1,1),2),((2,2),3)]
现在我希望将最后一个元组中的 3 与某个数字相乘。我怎样才能做到这一点?
解决方案
如果你想乘以5
它会是:
accum (*) a [((2,2),5)]
-- ^ ^ ^ ^
-- function to combine values
-- array to read
-- one of the indices to manipulated
-- value to give to f for the associated index
该函数的签名为
accum :: Ix i => (e -> a -> e) -> Array i e -> [(i, a)] -> Array i e
它的文档说:
accum f 接受一个数组和一个关联列表,并使用累加函数 f 将列表中的对累加到数组中。
因此,调用accum f arrayValue listWithPairsOfIndicesAndValues
将调用在提供旧值和来自该索引的值时f
给出的每个索引,并返回一个新数组,其中提到的所有位置都更新为相应调用返回的值。listWithPairsOfIndicesAndValues
listWithPairsOfIndicesAndValues
listWithPairsOfIndicesAndValues
f
推荐阅读
- c++ - 跨平台库究竟如何适用于不同的机器
- python - How to do Categorical in pandas
- python - Is this a circular import?
- c# - I have an issue with a contact form. Works on localhost. When deployed its giving error when attempting to post it via the submit button
- java - Make recursive API call using Retrofit
- javascript - Reactjs 变量在 useEffect 之后返回未定义
- python - 如何显示图像并在 1.5 秒后将其杀死?
- vue.js - Vue 3:将 Options API 组件转换为 Composition API(将容器设置为元素 - this.$el vs ref?)
- blockchain - 为什么我的以太坊智能合约交易在 X 笔交易后没有显示?
- c++ - 这个递归案例有什么问题?