Tuesday, November 27, 2007

Current Fixation: Haskell

I tend to jump from pet project to pet project. My last project was to develop a roguelike with a completely different playing style. Also, all the code in all the major open source roguelikes is spaghetti. Some of it is so bad I'm amazed it works at all. I wanted to see if I could do better.

I started writing it in C because there is a possibility I would open source something like that and C tends to get more contributions. The problem is that as I wrote, and as my code grew, despite all my efforts to the contrary it became progressively messier. It also became increasingly harder to modify something in the base behavior without having to rewrite things that interact with it.

I also saw that it was going to get worse as time progressed. At first I was just trying to place rooms randomly and then passages between those rooms in realistic fashion. Soon it would be monsters attempting to find paths to flee and chase and plan out how to attack. It was either going to be completely unmanageable or I'd have to make huge sacrifices as to what features I wanted in this game.

I eventually stopped coding and started looking for a solution. That lead to my current fixation: Haskell.

I call it a fixation, but in truth I'm really, really impressed by it. So impressed that I will probably be using haskell until some better language comes along, if that is even possible. By the way, this is from someone who absolutely loathed lisp.

Haskell is a functional language, like lisp. I never really got to the point where I understood the benefits of functional programming because lisp has so many other warts that it is impossible to see past them. Don't even get me started on it, but as far as I'm concerned lisp is the reason functional programming died young.

Haskell is a language I've attempted off and on for some time now. The process was similar to my adoption of Linux. I used it, couldn't do what I wanted and waited six months to try it again. On about the fourth attempt I finally see the light. It helps that ghc (haskell's main compiler) is becoming more advanced and good documentation is finally proliferating.

Haskell is a pure functional language, and that lends a whole lot of advantages to it. It can perform concurrency and parallelism effortlessly and efficiently. You can often look at a function prototype and guess what it does. Its type system is very strict, but I'm seeing the advantages already, and I've only been using it for a month total. Entire classes of bugs are nonexistent. I just can't cause them. Most are eliminated before the program compiles due to the type system. All the code is clean. Unlike lisp I can look at something I did weeks ago and tell exactly what it does. I can also read other peoples' code. It is also very compact. About 1000 lines of C code generally compile down to 100 of haskell.

Bereft of an interesting project to take on, I started rewriting my roguelike in haskell. I haven't gotten that far just yet, mostly just area generation. But I did it in a comparable time frame and the resulting code is so easy to understand and intuitive. Also my preliminary results say it is very fast.

Some people who have attempted haskell claim it is too hard. I would have been one of those people six months ago. The problem is not haskell but rather the people who designed the language. As researchers and academics, they tend to think mathematical explanations are all that is needed. Unfortunately for people like me who are practical and imperative minded this is not sufficient. You just don't need to understand the math. There is good documentation out there, but it is hard to find amongst piles academic research papers. Also one of the main concepts "Monads" seems extremely hard to grasp when you first come across it. In truth monads are very simple, but finding the good documentation on them was a real bastard.

If you are really having trouble with monads like I was, I would like to suggest the first part of this research paper which is more like a tutorial called "Tackling the Awkward Squad" link

I think I'll devote a few future posts to examples from whatever code I'm working on. It is something you have to see to understand. I believe people should give this language some serious consideration.

No comments: