【问题标题】:Haskell data type error PBMfileHaskell 数据类型错误 PBMfile
【发布时间】:2012-05-01 21:06:43
【问题描述】:

我正在做作业,有一个错误

我必须对现在描述的数据类型做一个函数

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 个参数?我做错了什么?

【问题讨论】:

    标签: image haskell rgb ppm


    【解决方案1】:

    您的函数定义 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

    【讨论】:

      【解决方案2】:

      你需要括号,

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

      否则它将被解析为negative 的四个参数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-08
        • 2013-09-04
        • 2013-12-18
        • 1970-01-01
        • 1970-01-01
        • 2018-11-29
        相关资源
        最近更新 更多