Question

i am doing my homework and have an error

I have to do a functions about a data type described now

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

And his show functions

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)

And his load and apply function

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

When i try to do a function, for example

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

Hugs error

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

But PBM x y z are not 3 arguments? What am i doing wrong?

Was it helpful?

Solution

Your function definition negative PBM x y z is trying to pattern match against 4 arguments, the first of which is the PBM data constructor. To actually pattern match the data constructor and its arguments, you should be grouping them, i.e. negative (PBM x y z) = .... The show definition in your question is an example of doing it correctly.

For further reading, try http://en.wikibooks.org/wiki/Haskell/Pattern_matching#The_connection_with_constructors.

OTHER TIPS

You need parentheses,

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

otherwise it is parsed as four arguments to negative.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top