Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

haskell examples often do not work

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?

like image 684
R71 Avatar asked Jan 17 '23 16:01

R71


1 Answers

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.

like image 195
ehird Avatar answered Feb 01 '23 08:02

ehird