我一整天都在尝试编译 Haskell 代码——再次——涉及 Control.Monad.Writer。这是一个无法编译的代码示例向你学习 Haskell http://learnyouahaskell.com/for-a-few-monads-more:
import Control.Monad.Writer
gcd' :: Int -> Int -> Writer [String] Int
gcd' a b
| b == 0 = do
tell ["Finished with " ++ show a]
return a
| otherwise = do
tell [show a ++ " mod " ++ show b ++ " = " ++ show (a `mod` b)]
gcd' b (a `mod` b)
我收到此错误:
No instance for (Show (Writer [String] Int))
arising from a use of `print'
Possible fix:
add an instance declaration for (Show (Writer [String] Int))
In a stmt of an interactive GHCi command: print it
我尝试编译老师今天编写的代码,也涉及 Control.Monad.Writer 但没有任何效果。
我正在使用 Ubuntu 12.04、gedit 和 GHC 7.4.1。
所有 Writer monad 程序来自向你学习 Haskell编译失败,我陷入了困境。
您显然输入了类似的内容
ghci> gcd' 12345 6789
在 ghci 提示符下。因此,您要求 ghci 打印 type 的值Writer [String] Int
,但是没有Show
实例为Writer
类型,因此 ghci 无法打印它。您需要申请runWriter
或类似的功能,
ghci> runWriter $ gcd' 12345 6789
应该管用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)