在haskell中只替换列表的头?
我有一个类似于如下的清单,我有一个类似如下的清单,如:<代码>[0,0,0 0,0 0,0,0,0 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1。我该怎么做 编辑: 我有这个函数,返回在haskell中只替换列表的头?,haskell,Haskell,我有一个类似于如下的清单,我有一个类似如下的清单,如:[0,0,0 0,0 0,0,0,0 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1。我该怎
[[0,0,0,1,0],[1,0,0,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]
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))
firstfunc::(RandomGen g)=>g->Int->Float->[[Int]]
firstfunc rnd n p=makGrid$map(\t->if t如果您想要一种转换列表头的通用方法,您可以创建一个函数:
transformHead :: (a -> a) -> [a] -> [a]
transformHead _ [] = []
transformHead f (x:xs) = (f x):xs
然后您可以使用它来转换来自firstFunc
的结果,例如
transformHead (map (\x -> 2 - x)) $ firstfunc gen c p
replaceFirst((replace.)newHead=newHead:xs
?我们不能将其添加到上面的函数中吗?我的意思是,我是从它调用replace还是什么?您可以像我在上一个问题中向您展示的那样执行…((replace.).firstfunc)
,或者等效地替换$firstfunc gen 10.3
。完美!谢谢!
transformHead (map (\x -> 2 - x)) $ firstfunc gen c p