【发布时间】:2015-08-04 13:50:18
【问题描述】:
我正在尝试执行 Graham Hutton 的 Programming in Haskell 书 (http://www.cs.nott.ac.uk/~gmh/book.html) 中的示例。
即使这些示例是在 literate haskell 中,我也可以启动 ghci 来加载示例;例如ghci cipher.lhs (http://www.cs.nott.ac.uk/~gmh/cipher.lhs):
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Main ( cipher.lhs, interpreted )
Ok, modules loaded: Main.
*Main> let2int 'a'
0
但是对于一些例子,由于 ghci 的变化,我有一些问题;例如在第 8 章的 Parsing.ls 中,我有 No instance for (Applicative ...) 错误。
来自https://ghc.haskell.org/trac/ghc/wiki/Migration/7.10 我得到了一些提示,可以通过添加一些代码来消除一些错误。
> instance Applicative Parser where
> pure = return
> (<*>) = ap -- defined in Control.Monad
>
> instance Functor Parser where
> fmap = liftM
>
> instance Alternative Parser where
> (<|>) = mplus
> empty = mzero
但是,我无法解决此错误消息:
Not in scope: type constructor or class ‘Alternative’
这有什么问题,以及如何解决这个问题? 导致问题的原始代码来自:http://www.cs.nott.ac.uk/~gmh/Parsing.lhs
解决方案
添加此代码可以正常工作:
import qualified Control.Applicative as CA
instance CA.Alternative Parser where ...
【问题讨论】:
-
很高兴我们终于可以一起解决这个问题 - 恭喜
标签: haskell literate-programming