Haskell 示例通常不起作用
我是 haskell 的初学者,使用最新的 GHCi 2011.2.0.1。我经常面临的一个挫折是,互联网和书籍中的示例(甚至来自官方 haskell.org 网站,例如 http://www.haskell.org/haskellwiki/State_Monad) 不编译。并且需要进行几轮搜索和发布问题才能找出问题所在。甚至 RWH 书中的一些例子也不起作用。
这是新版本的 haskell 或 haskell 平台的问题吗?语言/平台是否正在开发且不稳定,或者只有示例已过时?不管怎样,对于新学生来说,找到正确的例子来学习的最佳方法是什么?
更具体地说,你能帮助修复上面提到的状态 monad 示例吗?
I am a beginner in haskell, and using the latest GHCi 2011.2.0.1. One frustration I face frequently is that very often the examples in the internet and books (even from the official haskell.org site, for instance the example-2 of http://www.haskell.org/haskellwiki/State_Monad) dont compile. And it takes several rounds of hunting and posting questions to figure out what the problem is. Even some of the examples from the RWH book dont work.
Is this a problem with new version of haskell or of the haskell-platform? Are the language / platform under development and unstable, or only the examples are obsolete? Either way, what is the best way for a new student to find correct examples to learn from?
More specifically, can you help to fix the state monad example mentioned above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,问题在于
State
构造函数已在最新版本的 Monad Transformer 库中删除。如果将getNext
定义中的State
替换为state
,那么程序就可以正常工作。 (这是因为State
被重新定义为StateT Identity
,即身份单子上的状态单子转换器,因此单独的数据类型已被删除。如果您不这样做'不知道这意味着什么,不用担心;它只是意味着无论何时您看到它都必须将State
替换为state
。)现有的大多数问题网络或 RWH 中的示例仅仅是由于新版本的图书馆。 (我认为 RWH 示例的主要问题是 Parsec 示例是为 Parsec 2 编写的;Parsec 3 发生了很大变化,因此至少您可能需要添加一些导入。)
一般来说,Haskell 确实存在以下问题没有足够的最新、有用的资源; HaskellWiki 页面似乎没有维护。 (虽然 HaskellWiki 确实是官方 haskell.org 网站,但页面是由用户编写和维护的,因此 wiki 上的内容并不一定意味着它是最新的或高质量的。)
Monad Transformer Library删除了
State
的版本于 2010 年 10 月发布,因此遗憾的是仍然有很多代码因此无法在新版本中使用。值得庆幸的是,修复方法很简单。In this case, the problem is that the
State
constructor has been removed in newest version of the Monad Transformer Library. If you replace the use ofState
in the definition ofgetNext
withstate
, then the program works just fine. (This is becauseState
was redefined asStateT Identity
, i.e. the state monad transformer over the identity monad, and so the separate data-type has been removed. If you don't know what this means, don't worry about it; it just means that you have to replaceState
withstate
whenever you see it.)Most of the problems with existing examples on the web or in RWH are simply due to new versions of libraries. (I think the main problem with RWH's examples is that the Parsec examples are written for Parsec 2; Parsec 3 changed quite a bit, so at the very least you'll probably need to add some imports.)
In general, Haskell does suffer from not having enough up-to-date, helpful resources; that HaskellWiki page just seems to be unmaintained. (While the HaskellWiki is indeed the official haskell.org site, the pages are written and maintained by the users, so just because something's on the wiki doesn't necessarily mean it's up-to-date or high quality.)
The Monad Transformer Library release that removed
State
was released in October 2010, so it's a shame that there's still a lot of code out there that doesn't work with the new version because of this. Thankfully, the fix is simple.