相关疑难解决方法(0)

如何在递归函数中使用Control.Monad.Cont?

我正在提供这个问题的答案,我想到了使用Contmonad 的想法.我不知道Haskell足以解释为什么这个程序不起作用

import Control.Monad.Cont

fib1 n = runCont (slow n) id
  where
    slow 0 = return 0
    slow 1 = return 1
    slow n = do
      a <- slow (n - 1)
      b <- slow (n - 2)
      return a + b

main = do
  putStrLn $ show $ fib1 10
Run Code Online (Sandbox Code Playgroud)

错误 -

main.hs:10:18: error:
    • Occurs check: cannot construct the infinite type: a2 ~ m a2
    • In the second argument of ‘(+)’, namely ‘b’ …
Run Code Online (Sandbox Code Playgroud)

monads continuations haskell type-mismatch do-notation

1
推荐指数
1
解决办法
97
查看次数