2014-12-04 108 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