我有一个类似:[[0,0,0,1,0],[1,0,0,1,0],[0,0,1,1,0],[0,0,0,0,0],[1,0,0,0,1]]
的列表,我希望将此列表的头部[0,0,0,1,0]
替换为[2,2,2,1,2]
,以获取[[2,2,2,1,2],[1,0,0,1,0],[0,0,1,1,0],[0,0,0,0,0],[1,0,0,0,1]]
。我怎么能这样做?
编辑:
我有此功能,返回[[0,0,0,1,0],[1,0,0,1,0],[0,0,1,1,0],[0,0,0,0,0],[1,0,0,0,1]]
,我希望它返回[[2,2,2,1,2],[1,0,0,1,0],[0,0,1,1,0],[0,0,0,0,0],[1,0,0,0,1]]
。
firstfunc :: (RandomGen g) => g -> Int -> Float -> [[Int]]
firstfunc rnd n p = makGrid $ map (\t -> if t <= p then 1 else 0) $ take (n*n) (randoms rnd)
where makGrid rnd = unfoldr nextRow (rnd, n)
nextRow (_, 0) = Nothing
nextRow (es, i) = let (rnd, rest) = splitAt n es in Just (rnd, (rest, i-1))
答案 0 :(得分:1)
change 0 = 2
change x = x
replace (x:rest) = map change x:rest
答案 1 :(得分:0)
如果你想要一种改变列表头部的一般方法,你可以创建一个函数:
transformHead :: (a -> a) -> [a] -> [a]
transformHead _ [] = []
transformHead f (x:xs) = (f x):xs
然后你可以用它来转换firstFunc
例如
transformHead (map (\x -> 2 - x)) $ firstfunc gen c p