Haskell语言Array模块中函数accumArray的用法及代码示例。
用法类型:
Ix a => (b -> c -> b) -> b -> (a,a) -> [(a,c)] -> Array a b
通过使用将具有相同索引的关联值组合在一起的累加函数,放宽了给定索引最多只能在关联列表中出现一次的限制。第一个论点accumArray是累加函数;第二个是初始值;其余两个参数是一个边界对和一个关联列表,与数组函数一样。
示例1:
输入:
accumArray (+) 0 (1,3) [(1,2),(1,4),(2,1),(2,2),(2,3),(3,5)]
输出:
array (1,3) [(1,6),(2,6),(3,5)]
示例2:
输入:
accumArray (++) "" (1,3) [(1,"H"),(3,"y"),(1,"i"),(2,","),(3,"ou")]
输出:
array (1,3) [(1,"Hi"),(2,","),(3,"you")]
示例3:
输入:
accumArray (\x y-> x ++ y ++ "-") "XXX:" (1,3) [(1,"H"),(3,"y"),(1,"i"),(2,","),(3,"ou")]
输出:
array (1,3) [(1,"XXX:H-i-"),(2,"XXX:,-"),(3,"XXX:y-ou-")]
相关用法
注:本文由纯净天空筛选整理自 haskell accumArray。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。