可能的重复:
替换 Haskell 中的单个列表元素? https://stackoverflow.com/questions/5852722/replace-individual-list-elements-in-haskell
我已经在这部分作业中取得了一些进展,但附上了我所做的部分代码:
module Grid where
data State = On | Off deriving (Eq, Show)
next :: State -> State
next On = Off
next Off = On
type Row = [State]
updateRow :: Row -> Int -> Row
updateRow (r:rs) x
| x == 0 = next r:rs
-- | otherwise = ........????
如上面最后一行所示,我成功地让 updateRow 在 x = 0 时工作,如下所示(第 0 个元素反转)。
*Grid> updateRow [Off,Off,Off,Off] 0
[On,Off,Off,Off]
*Grid>
然而,当我尝试反转此列表的其他元素时,一切都变得不那么困难。我似乎无法“概括”这个函数中的公式。
我还必须遵循这种类型约定:
updateRow :: Row -> Int -> Row
提前致谢。
像这样的东西:
module Grid where
data State = On | Off deriving (Eq, Show)
next :: State -> State
next On = Off
next Off = On
type Row = [State]
updateRow :: Row -> Int -> Row
updateRow (r:rs) x
| x == 0 = next r:rs
| otherwise = r : (updateRow rs (x-1))
updateRow [] x = []
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)