我是haskell 的新手,我正在尝试编写我的第一个haskell C 库。这是我第一次使用 Foreign.C 模块。我在示例中迷失了方向并且陷入了困境。到目前为止,这是我想出的:
{-# LANGUAGE ForeignFunctionInterface #-}
module Grep where
import GHC.Ptr
import Foreign.C.String
import Data.List (isInfixOf)
grep :: CString -> CString -> CString
grep i s = do
ii <- peekCString i
ss <- peekCString s
g <- newCString (isInfixOf ii ss)
g
foreign export ccall grep :: CString -> CString -> CString
我收到以下错误:
PS C:\Users\GGuy\Source\haskell> ghc -c -O grep.hs
grep.hs:11:9:
No instance for (Monad Ptr)
arising from a do statement
Possible fix: add an instance declaration for (Monad Ptr)
In a stmt of a 'do' block: ii <- (peekCString i)
In the expression:
do { ii <- (peekCString i);
ss <- peekCString s;
g <- newCString (isInfixOf ii ss);
g }
In an equation for `grep':
grep i s
= do { ii <- (peekCString i);
ss <- peekCString s;
g <- newCString (isInfixOf ii ss);
.... }
grep.hs:11:16:
Couldn't match expected type `Ptr t0' with actual type `IO String'
In the return type of a call of `peekCString'
In a stmt of a 'do' block: ii <- (peekCString i)
In the expression:
do { ii <- (peekCString i);
ss <- peekCString s;
g <- newCString (isInfixOf ii ss);
g }