2

我正在尝试为 C 库构建一个包装器。它有一些我以前从未见过的带有问号的奇怪语法:

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 ‘?’

抬头看“?” 在 Hoogle 上发现了一些关于“隐式参数”的东西,但这似乎并不相关。我尝试用类型孔替换神秘的问号:

 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);
           .... }

我真的不确定如何进行。有小费吗?

更新:我找到了原始仓库,它似乎已经解决了这个问题,但我仍然想知道问号是什么?

4

1 回答 1

3

?不是语法,它是haskell中的有效运算符。但在这里它只是一个“这里有一些魔力”的东西。

如上所述,作者只是忘记用一些涉及pixelVals'. 最有可能的是,它应该是CImage从该像素值构造参数。

Ghc 的解析器将给出parse error由运算符符号组成的任何两个连续标记(除了-) - 因为它在第二个问号之后失败。

于 2014-12-25T11:21:25.723 回答