Dic*_*eed 3 monads haskell currying
我收到了来自ghc的错误消息,我没有发出请求,并将我的代码缩减为:
import System.Process
main = do
(_, out, _) <- readProcessWithExitCode "echo" ["foo"]
putStr out
Run Code Online (Sandbox Code Playgroud)
(我应该给readProcessWithExitCode一个额外的参数).使用runghc编译破坏的程序给出:
Test.hs:4:2:
Couldn't match expected type `IO
(GHC.IO.Exception.ExitCode, String, String)'
against inferred type `(a, b, c)'
In the pattern: (_, out, _)
In a stmt of a 'do' expression:
(_, out, _) <- readProcessWithExitCode "echo" ["foo"]
In the expression:
do { (_, out, _) <- readProcessWithExitCode "echo" ["foo"];
putStr out }
Run Code Online (Sandbox Code Playgroud)
我怎么知道我没有完全应用此ghc错误消息中的函数?
iva*_*anm 10
如果您指定了类型签名main :: IO (),则错误将变为:
test.hs:5:18:
Couldn't match expected type `IO t0'
with actual type `String
-> IO (GHC.IO.Exception.ExitCode, String, String)'
In the return type of a call of `readProcessWithExitCode'
In a stmt of a 'do' expression:
(_, out, _) <- readProcessWithExitCode "echo" ["foo"]
In the expression:
do { (_, out, _) <- readProcessWithExitCode "echo" ["foo"];
putStr out }
Run Code Online (Sandbox Code Playgroud)
在这种情况下,错误更加明显.
我的猜测是错误不是很明显,因为在推断类型时,GHC认为你处于函数(即(a->))monad而不是IO,在这种情况下你将在之后应用参数.
所以不要偷懒,并包括那些类型的签名!;-)