Haskell 示例通常不起作用

发布于 2024-12-29 04:52:05 字数 417 浏览 2 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我老 2025-01-05 04:52:05

在这种情况下,问题在于 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 of State in the definition of getNext with state, then the program works just fine. (This is because State was redefined as StateT 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 replace State with state 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文