Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.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_Monads - Fatal编程技术网

Haskell 如何将图像转换为颜色矩阵?

Haskell 如何将图像转换为颜色矩阵?,haskell,monads,Haskell,Monads,我正在玩Graphics.GD,我想把一幅图像读入Color值矩阵,有点像这样: rectFromImage :: Image -> IO [[Color]] rectFromImage img = do size <- imageSize img return [[getPixel (x,y) img | x <- [1 .. fst size]] | y <- [1 .. snd size]] 如何在getPixel调用的返回中“摆脱IO”?序列是您

我正在玩
Graphics.GD
,我想把一幅图像读入
Color
值矩阵,有点像这样:

rectFromImage :: Image -> IO [[Color]]
rectFromImage img = do
    size <- imageSize img
    return [[getPixel (x,y) img | x <- [1 .. fst size]] | y <- [1 .. snd size]]

如何在
getPixel
调用的返回中“摆脱IO”?

序列
是您正在寻找的神奇功能<代码>序列获取IO操作列表,并使其成为值的IO列表。在类型签名术语中:

sequence :: Monad m => [m a] -> m [a]
或者,更具体地说,在您的情况下:

sequence :: [IO a] -> IO [a]
因此,您可以这样做,例如:

do
  putStrLn "Enter three lines of input:"
  irritatedUser <- sequence [getLine, getLine, getLine]
  putStrLn (irritatedUser !! 2)
do
putStrLn“输入三行输入:
刺激剂
do
  putStrLn "Enter three lines of input:"
  irritatedUser <- sequence [getLine, getLine, getLine]
  putStrLn (irritatedUser !! 2)
rectFromImage img = do
  size <- imageSize img
  sequence [sequence [getPixel (x,y) img | x <- [1 .. fst size]] | y <- [1 .. snd size]]