下面的Haskell snippit不能编译,我不知道为什么。
runCompiler :: TC -> IO ()
runCompiler tc = let cp' = cp in
do
cp'
return ()
where
cp = compileProg tc我从GHCi得到以下错误:
Couldn't match expected type `IO a0' with actual type `String'
In a stmt of a 'do' block: cp'
In the expression:
do { cp';
return () }
In the expression:
let cp' = cp
in
do { cp';
return () }你有什么办法让它编译吗?我不明白为什么它不接受()作为给定的最终值。
发布于 2013-04-05 20:13:43
使用do表示法对两个语句进行排序时:
do
action1
action2与action1 >> action2相同
因为>>的类型是Monad m => m a -> m b -> m b,所以action1和action2都应该是一元值。
您的compileProg函数似乎具有类型TC -> String,而编译器希望它对于某些a为TC -> IO a,因为您在do表示法中使用它。
您可以使用let
do
let _ = compileProg tc
return ()来编译它。
如果要输出返回的字符串,可以使用putStrLn或print
do
putStrLn (compileProg tc)
return ()由于putStrLn的类型为String -> IO (),因此可以删除return ()
do
putStrLn (compileProg tc)实际上,runCompiler可以简单地写成
runCompiler :: TC -> IO ()
runCompiler = putStrLn . compileProghttps://stackoverflow.com/questions/15833459
复制相似问题