Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在haskell中只替换列表的头?_Haskell - Fatal编程技术网

在haskell中只替换列表的头?

在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 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