2014-12-04 107 views
0

即時新使用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)))))] 

對不起,我的英語不好,謝謝!

回答

1

以下功能:

aplicar_negativo2 :: [[RGBdata]] -> [[RGBdata]] 
aplicar_negativo2 [[RGB x y z]] = ([[RGB (255-x) (255-y) (255-z)]]) 

只匹配單個RGBdata項目,在一個列表,在列表....

然而,看着錯誤消息後(... 。那是不容易的,相信我),我能看到你調用此函數與

[[RGB _ _ _,RGB _ _ _] ++ <more stuff>] 

所以你在名單內至少有兩個元素通過一些形式的東西,INT一個函數,只會在該列表中佔用一個元素。

我的猜測是,你的意思是邏輯aplicar_negativo2適用於所有元素中....您可以通過定義功能應用到一個元素,然後用map外(在您使用它)做到這一點。

+0

對不起,我是不活躍的,我不明白你的建議,我與[[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