【发布时间】:2011-04-21 16:45:54
【问题描述】:
美好的一天。
这是简单的“猜数字”sn-p,它包含单个错误,但编译器 真的很难理解哪里出了问题:
import System.Random
import Control.Monad
import Control.Monad.Cont
main = do
rn <- randomRIO (1,10) :: IO Int
runContT (callCC $ \k -> forever $ do
lift $ putStr "Your number:"
n <- lift (fmap read getLine)
when (n == rn) $ k
lift $ putStrLn $ if (n > rn)
then "Too much"
else "Too little") (return)
putStrLn $ "Good guess! " ++ (show rn)
GHC 给出错误:
> simple.hs:11:21:
> Couldn't match expected type `()' against inferred type `m b'
> Expected type: a -> ()
> Inferred type: a -> m b
> In the second argument of `($)', namely `k'
> In a stmt of a 'do' expression: when (n == rn) $ k
这让我很困惑,它告诉了一些预期的类型“()”,但是如何 发现这个“东西”是谁?是“k”吗?似乎不是这样。 如果我们交换预期和推断,它看起来更理智,但现在看起来,它非常令人困惑。我的问题是:如何找出原因并修复此错误?
【问题讨论】:
-
即 `k' 在这里应该是一个很大的提示...
标签: haskell types type-inference ghc