【发布时间】:2012-10-08 23:45:11
【问题描述】:
我有以下功能:
-- xs: list to be changed
-- ws: list of indices where the values will change to 0
replaceAtIndices xs ws = [if (fromJust (elemIndex x xs)) `elem` ws then 0 else x | x <- xs]
该函数接受 2 个列表。 ws 是 xs 中我要更改为 0 的值的索引。
出于某种原因,它适用于某些情况,而不适用于其他情况:
*Main> replaceAtIndices [1,2,3,4] [2,3]
[1,2,0,0] -- 正确
*Main> replaceAtIndices [1,2,3,4] [2]
[1,2,0,4] -- 正确
*Main> replaceAtIndices [1,1,2,1,3] [3]
[1,1,2,1,3] -- 应该是 [1,1,2,0,3]
谁能解释一下这是为什么?
提前致谢!
【问题讨论】:
-
我编辑了标题,因为问题与Monad界面无关;
Maybe只是一种数据类型。
标签: list function haskell maybe