2012-05-01 9 views
0

私は私の宿題をやってHaskellのデータ型エラーPBMfile

エラーを持っています私は今

data RGBdata= RGB Int Int Int 
data PBMfile= PBM Int Int [[RGBdata]] 

そして、彼のショーの機能

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 

私は機能をしようとすると、例えば

negative :: PBMfile -> [Int] 
negative PBM x y z = [1,2,3] 

抱擁エラー

ERROR file:.\haha.hs:32 - Constructor "PBM" must have exactly 3 arguments in pattern 

しかし、PBMのxのy、zは3つの引数ではありませんか?何が間違っているのですか?

negative :: PBMfile -> [Int] 
negative (PBM x y z) = [1,2,3] 

あなたは括弧が必要

答えて

2

関数の定義negative PBM x y zは、4つの引数に対してパターンマッチングを試みています。最初はPBMデータコンストラクタです。実際にデータコンストラクタとその引数のパターンを一致させるには、グループ化する必要があります(negative (PBM x y z) = ...)。あなたの質問にshowの定義が正しく実行されています。

詳細については、http://en.wikibooks.org/wiki/Haskell/Pattern_matching#The_connection_with_constructorsをお試しください。

1

は、そうでない場合はnegative 4つの引数として解析されます。