我是新來的哈斯克爾,我試圖寫我的第一個哈斯克爾C庫。這是我第一次使用Foreign.C模塊。我迷失在實例中,並陷入困境。這就是我來了這麼遠:GHC - 沒有實例(Monad Ptr)
{-# 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 }
'isInfixOf'給出'Bool',而不是'''newCString'想要的'String' - 你想要做什麼? – dave4420
@ dave4420謝謝!完全錯過了。如果我在s中,我試圖返回true。由於沒有CBool我猜定義導出將是「CString - > CString - > Bool」...? – NerdGGuy
'CString - > CString - > IO Bool',在查看之後,您只需'返回$ ii'isInfixOf' ss''。 –