即時新使用Haskell,即時通訊做一個項目,我對數據類型有一些懷疑。(Haskell上的RGBData)模式匹配失敗
IM使用此數據類型
data RGBdata= RGB Int Int Int
data PBMfile= PBM Int Int [[RGBdata]]
也驗證碼,老師給了我們PBM文件和進出口工作
instance Show RGBdata where
show (RGB r g b)=(show r)++" "++(show g)++" "++(show b)
instance Show PBMfile where
show (PBM width height l)= "P3\n"++(show width)++" "++(show height)++"\n255\n"++(foldr (++) "" (map myshow l))
myshow [] = "\n"
myshow (h:t) = (show h)++" "++(myshow t)
cargarPBM name = readFile name >>= return . rLines . lines
rLines (_:x:_:xs)= (\[a,b] -> (PBM (read a) (read b) (rLines' (read a) (concat $map words xs)))) $ words x
rLines' _ [] = []
rLines' a x = (rLine (take (a*3) x): rLines' a (drop (a*3) x))
rLine []= []
rLine (r:g:b:xs)= ((RGB (read r) (read g) (read b)):rLine xs)
aplicar funcion origen destino= cargarPBM origen >>= writeFile destino . show . funcion
然後我需要做的一些功能與PBM文件,像轉換它爲負數,旋轉它等。 即時開始使PBM爲負,轉換爲負數(RGB 255-R 255-G 255-B)
i di d是一個使PBM文件爲負數的函數!這裏是代碼,我沒有..
negativo :: PBMfile -> PBMfile
negativo (PBM i j mtx) = (PBM i j (aplicar_negativo 0 (i*j) mtx))
aplicar_negativo :: Int -> Int -> [[RGBdata]] -> [[RGBdata]]
aplicar_negativo a n (x:xs) | ((a+2)==n) = (aplicar_negativo2 [x])++(aplicar_negativo2 xs)
| otherwise = (aplicar_negativo2 [x])++(aplicar_negativo (a+1) n xs)
aplicar_negativo2 :: [[RGBdata]] -> [[RGBdata]]
aplicar_negativo2 [[RGB x y z]] = ([[RGB (255-x) (255-y) (255-z)]])
這3個功能只是更改所有rgbdatas 255-R,255-G和255-B .. 也就是說,如果我有我的名單列表[RGB 100 200 100],[RGB 50 55 50] 結果是:[RGB 155 55 155],[RGB 205,200,205]
這裏是當我申請,我得到錯誤在圖像上的函數negativo:
Program error: pattern match failure: aplicar_negativo2 [[RGB (read (_SEL (,)
("6" ++ _SEL (,) ("5" ++ _SEL (,) ([],[]) 1,[]) 1,[]) 1)) (read (_SEL (,) ("1"
++ _SEL (,) ("2" ++ _SEL (,) ("6" ++ _SEL (,) ([],[]) 1,[]) 1,[]) 1,[]) 1))
(read (_SEL (,) ("7" ++ _SEL (,) ("1" ++ _SEL (,) ([],[]) 1,[]) 1,[]) 1)),RGB
(read (_SEL (,) ("5" ++ _SEL (,) ("2" ++ _SEL (,) ([],[]) 1,[]) 1,[]) 1)) (read
(_SEL (,) ("1" ++ _SEL (,) ("1" ++ _SEL (,) ("2" ++ _SEL (,) ([],[]) 1,[]) 1,[])
1,[]) 1)) (read (_SEL (,) (words_v858 (break isSpace "5" ++ _SEL (,) (span_v848
(span (not . primEqChar '\n') (_hreader {handle}))) 1)) 1))] ++ rLine (take
(700 - 1) (words (_SEL (,) (words_v858 (break isSpace "5" ++ _SEL (,) (span_v848
(span (not . primEqChar '\n') (_hreader {handle}))) 1)) 2) ++ foldr (++) []
(map_v810 words (lines_v853 (_SEL (,) ("5" ++ _SEL (,) (span_v848 (span (not .
primEqChar '\n') (_hreader {handle}))) 1,_SEL (,) (span_v848 (span (not .
primEqChar '\n') (_hreader {handle}))) 2) 2)))))]
對不起,我的英語不好,謝謝!
對不起,我是不活躍的,我不明白你的建議,我與[[RGBdata]]一起工作,我用圖像比第一次嘗試..這裏是結果! aplicar negativo 「Prueba.pbm」 「resnegativo2.pbm」 **程序錯誤:圖案匹配失敗:rLines_v1623(lines_v853(_SEL(,)(lines_v852(斷裂( '\ n' ==) 「\ ENQ」 + + _hreader {handle}))2))[_SEL(,)(「\ ENQ」++ _SEL(,)(「\ EOT」++ _SEL(,)([],[])1,[])1 ,[])1] ** – 2014-12-06 20:45:04