Haskell中的问号关键字

时间:2014-12-24 19:17:54

标签: opencv haskell flycapture

I'm trying to build a wrapper for a C library.它有一些奇怪的语法,带有我以前从未见过的问号:

cvLoadImage :: Capture -> IO CImage
cvLoadImage capture = do
   (Just imageRGB) <- getFrame capture
   let imageD32 = rgbToGray imageRGB
   let (CArray (nx0, ny0) (nx1, ny1) numElem values) = copyImageToFCArray imageD32
   pixelVals <- (withForeignPtr values) (peekArray numElem)
   let pixelVals' = map (fromIntegral . truncate . (*255)) pixelVals
   ptr <-( mallocArray (numElem) ::IO (Ptr Word8))
   pokeArray ptr pixelVals'
   return CImage (? ? ? pixelVals' ? ? ? ?) -- this is the line I'm confounded by

编译时导致语法错误:

src/System/FlyCap.hs:185:21: parse error on input ‘?’

向上看&#34;?&#34;在Hoogle上找到了关于'implicit parameters'的内容,但这似乎并不相关。我尝试用类型孔替换神秘的问号:

 return CImage (_ _ _ pixelVals' _ _ _ _)

这给了我一个更大的错误:

src/System/FlyCap.hs:186:4:
    Couldn't match type ‘IO CImage’
                  with ‘CUInt
                        -> CUInt
                        -> CUInt
                        -> Ptr CUChar
                        -> CUInt
                        -> CInt
                        -> CInt
                        -> Ptr ()
                        -> CImage’
    Expected type: t6 -> IO CImage
      Actual type: t6
                   -> CUInt
                   -> CUInt
                   -> CUInt
                   -> Ptr CUChar
                   -> CUInt
                   -> CInt
                   -> CInt
                   -> Ptr ()
                   -> CImage
    The function ‘return’ is applied to two arguments,
    but its type ‘(CUInt
                   -> CUInt
                   -> CUInt
                   -> Ptr CUChar
                   -> CUInt
                   -> CInt
                   -> CInt
                   -> Ptr ()
                   -> CImage)
                  -> t6
                  -> CUInt
                  -> CUInt
                  -> CUInt
                  -> Ptr CUChar
                  -> CUInt
                  -> CInt
                  -> CInt
                  -> Ptr ()
                  -> CImage’
    has only 10
    In a stmt of a 'do' block: return CImage (_ _ _ pixelVals' _ _ _ _)
    In the expression:
      do { (Just imageRGB) <- getFrame capture;
           let imageD32 = rgbToGray imageRGB;
           let (CArray (nx0, ny0) (nx1, ny1) numElem values)
                 = copyImageToFCArray imageD32;
           pixelVals <- (withForeignPtr values) (peekArray numElem);
           .... }

我真的不确定如何继续。有什么提示吗?

更新:我找到original repo, which seems to have fixed this,但我仍然想知道问号是什么?

1 个答案:

答案 0 :(得分:3)

?不是语法,它是haskell中的有效运算符。但这里只是一个&#34;一些魔法在这里&#34;的事情。

如上所述,作者忘了用一些涉及pixelVals'的表达来替换它。最有可能的是,它应该是从像素值中构造CImage参数(s?)。

Ghc的解析器将对由运算符符号构成的任何两个序列标记赋予parse error-除外) - 因为它在第二个问号后失败。