37 Reasons to Love Haskell (playing off the Ruby article)
Here’s an article on reasons to like Ruby. It’s actually very well written and persuasive. So I took it as a challenge. How well would Haskell fare when compared against this specific set of criteria, changing the points as little as possible? The meanings will inevitably shift a bit, and readers will know that Haskell has its own set of advantages, of course.
The point of this exercise, though, is to see how Haskell does in the benefits mentioned for Ruby in that particular article. It’s not a Haskell vs. Ruby; just a shameless theft of a set of criteria. Should be fun.
- It’s object oriented. Although it wasn’t designed for it, this paper points out that Haskell scores pretty well in support for object oriented programming features. It’s not entirely clear that this is a good way to write code; but if it isn’t, it’s not because Haskell doesn’t provide the features or support you need to make it work.
- It’s pure. Haskell chooses to be pure in the functional sense, rather than the object-oriented sense. The same ideas remain, though; there are no messy bits that don’t fit into the prevailing mode of thought.
- It’s a dynamic language. Yep, you heard right. Projects like Yi and hs-plugins and lambdabot prove that it’s quite possible to write programs with runtime code loading and manipulation and other dynamic features in Haskell. Indeed, the type system gives you a level of assurance that you haven’t made big mistakes along the way; something that’s quite likely when assembling code at runtime from many small pieces.
- It’s an interpreted language. Honestly, I’m having trouble understanding this point in the original; the Ruby article doesn’t ever say why this is a good thing. It helps, of course, in that it lets you work with the program interactively, easily trying out bits at a time rather than having to write a new main method to do any testing. Unlike Ruby, Haskell can also be compiled to handle any performance concerns; combining the advantages of both worlds.
- It understands regular expressions. The
Text.Regexmodule in the Haskell standard library contains functions to do regular expression stuff. It even uses Haskell’s operator mechanism to define operators like
=~to look more like other languages sometimes.
- It’s multi-platform. GHC, the major Haskell compiler, exists for Linux (many processors and distributions), Other UNIX-like systems (FreeBSD, NetBSD, OpenBSD, Solaris), many Windows variants (95 through Vista), and MacOS (Intel and PowerPC). Nobody cares about MS-DOS :).
- It’s derivative. Haskell as a language borrows many of the best features from other languages, especially ML (its type system), and Miranda (its evaluation order). It borrows libraries from lots of places. There are Haskell bindings for wxWindows and GTK+, for example. And yes, printf, too.
- It’s innovative. If there’s any single widely used language today that has a claim to being truly innovative, it’s Haskell. Haskell is practically a research playground, while still managing to be a practical language. New language features have been the bread and butter of its progress: type classes, monadic I/O and computational environments; more recently: GADTs, associated types. All of these are rare in other languages.
- It’s a Very High-Level Language (VHLL). I actually doubt this term has any kind of meaning at all; but if it does, then Haskell has quite a good claim to fitting its meaning. To the extent that the high/low level language distinction is meaningful, it’s about the ability of software to transform the program from something that’s useful to programmers into something that’s efficient and executable on machines. More work is going on here in Haskell (e.g., see the Data Parallel Haskell project) than in any other language I’m aware of.
- It has a smart garbage collector. I’m not sure this should even be worth a mention on the list. Languages without automatic memory management are simply not candidates for writing serious application level code in the modern world. For what it’s worth, though, yes Haskell does it.
- It’s a scripting language. Despite having virtually none of the properties that conventional wisdom associates with scripting languages, Haskell is quite usable for scripting. Function composition and the monadic
>>=operator provide the ability to combine pieces every bit as tersely as pipes; type inference eliminates the cost of static types. This page talks about using Haskell for scripting, and this one describes the
-eoption to ghc, which lets you run Haskell code directly from the command line, and gives an example of using it.
- It’s versatile. As a general purpose programming language, you can do as much with Haskell as practically anything. Scripting is simple, as described above. Haskell also has a very advanced application server letting you write complex web applications; has been used to write a very nice window manager, is widely used in financial and other industries, and has been used to implement other languages — itself and the very first implementation of Perl 6.
- It’s thread-capable. And more! There are basically two languages worth looking at for modern concurrent programming: Erlang and Haskell. Haskell implements not just multithreading, but three different higher-level abstractions on top of multithreading: Software Transactional Memory, Data Parallel Haskell, and basic Parallel Haskell. All three provide tools to make it easier to build correct threaded programs. (Of course, Haskell offers less interesting traditional concurrency abstractions as well.)
- It’s open-source. All Haskell implementations have the source code fully available. The major people involved hang out and regularly respond by both mailing lists (newsgroup interface available, too) and even IRC channels. Haskell has one of the most famously open and friendly communities around, so you’ll fit right in!
- It’s intuitive. Haskell doesn’t have a shallow learning curve, but that’s because you’re really learning things; not just learning a new syntax for the same programming you’ve done for years. Haskell stays out of the way and lets your mind be expanded by the concepts you’re seeing instead of by the arbitrary choices of the language implementors. That’s about as intuitive as you can really ask for.
- It handles exceptions well. Unlike practically any other language, Haskell does the right thing for computations that have exceptional cases. When working in a purely functional way, it gives you simple types like
Maybethat help to express those exceptional conditions in a functional manner. When you’re working in an imperative way (e.g., in the
IOmonad, or any other
MonadPlusenvironment) it provides exception handling, since that’s the right choice for the imperative style.
- It has an advanced Array type. You don’t have to declare types at compile-time, or allocate memory in advance, or keep up with their length, or worry about indexing out of bounds (unless you explicitly choose to use unsafe operations). Unlike many other languages, though, arrays aren’t syntactically preferred. It’s just as easy to use a map, list, sequence, or many other things depending on what best suits your application.
- It’s extensible. You can write external libraries in Haskell or in C. You can declare new instances (in other words add new behaviors to existing type signatures; gives you the core benefit of modifying existing classes) on the fly. You can also add new operators (and I do mean new operators, not rehashing a very limited set of operators in error-prone ways like you do in C++) and use monads to define whole new computational environments if you like.
- It encourages literate programming. Even if you aren’t really doing literate programming, you can embed comments in your code which the Haddock tool will extract and manipulate. You can look at type information even if it was never documented at all, simply by asking GHCi or Hugs for the type. If you’re really into literate programming, though, major Haskell compilers understand literate source files that default to comments, and only include code delimited in specific ways (birdtracks, or latex begin/end commands). This lets you, quite literally, compile the same document into either executable code or a details PDF user manual, without even having to do anything unusual to your latex source!
- It uses punctuation and capitalization creatively. Haskell enforces a punctuation scheme that makes the meaning of code clearer. Types, classes, modules, and data constructors begin with upper-case letters. Variables (including type variables) start with lower-case letters. Fortunately, though, a lot of very important information is stored in the type — including, for example, whether a function result is Boolean or not, and even whether it destructively updates something! This information is available at your fingertips either by reading documentation or just by typing
:tat the GHCi command prompt, so you don’t have to repeat it over and over again in your code.
- (Some) reserved words aren’t. Haskell has very few reserved words compared to most other languages, because the language itself is conceptually simpler (not to be confused with easier to learn, as mentioned earlier). A few keywords aren’t reserved, but I can’t pretend that’s a good thing.
- It allows iterators. More generally, higher-order functions are useful in a large number of situations. When combined with lazy evaluation, there are a plethora of powerful techniques for handling collections of data in Haskell. Iterators loosely correspond to maps and folds, which are well supported and widely used.
- It has safety and security features. Haskell provides as advanced a set of fool-proof safety and security features as is found in basically any language. Its type system allows you to express security constraints (even rather generalized ones) in embedded domain-specific constructs that can be enforced by the compiler, so you never even attempt to run unsafe code! If a new kind of security issue arises, it’s generally possible to use the powerful type system to solve the problem without waiting for language support for something like tainted data.
- It (really) has no pointers. Unlike the trivial sense in which languages like Java are claimed to have no pointers by restricting the most dangerous operations on them, Haskell really has no pointers. (It’s worth noting that the Java language specification wasn’t fooled into the “no pointers” myth, as a peek as the second sentence of 4.3.1 in the 3rd edition spec will make clear.) In a pure functional language, there’s no difference between using pointers and actual values, so the compiler can make the decision based on performance concerns, rather than exposing the distinction between pointers and data to the programmer. This is part of Haskell’s being a higher level language.
- It pays attention to detail. This is one of those Orwellian newspeak things where the Ruby article I’m working from claims one thing (attention to detail) and then describes the opposite (extreme sloppiness). Haskell follows the real attention to detail picture here, though you can define your own synonyms (for values or types) if you like.
- It has a flexible syntax. Haskell has a truly flexible syntax in ways that Ruby can’t dream of. It allows programmers to embed domain-specific languages that are significantly and fundamentally different from the imperative model of Ruby. Parsers can be written by embedding context-free grammars right into the source code. Logic processing can be added by embedding Prolog-style inference rules into the language. An infinite supply of operators are available to facilitate these languages. Higher-order functions and monadic environments are available to make them work well.
- It has a rich set of libraries. Available libraries is one place where Haskell is way above practically all languages with similar community sizes, and in the ballpark of a lot of mainstream languages. There are libraries for practically anything, including several for GUI programming, web programming, transactional persistence, and plenty else besides. I wouldn’t want to try to list them all, so here.
- It has a debugger. Okay, so this is the biggest stretch yet. The development version of GHC (to become GHC 6.8) actually does have a debugger; but the debugging tools for released versions of Haskell are sketchy at best. This is improving.
- It can be used interactively. This is true in the sense that GHCi exists. It’s less true in the sense that someone can use it as their login shell. Yes, it’s possible; no, it’s probably not a good idea. This is interesting, though.
- It is concise. Haskell code is about comparable to many scripting languages. Sometimes it’s a little longer. Sometimes (generally when one can make good use of powerful abstraction techniques like monads and higher-order functions ina program of large enough size that it makes a difference) it’s a lot shorter. The xmonad window manager is about 500 lines of code.
- It is expression-oriented. As a purely functional language, of course it’s expression-oriented.
- It is laced with syntactic sugar. Haskell’s got all sorts of nice syntax in ways that really matter quite a lot: custom operators and fixity declarations, do blocks for monadic computation, etc.
- It has operator overloading. In fact, operator overloading in Haskell is far nicer than in most other languages, because you can make up your own operators. No more confusing bit shifting with I/O. Within a given context, an operator means a specific thing; but at the same time, it can apply to your custom types (ah, the magic of type classes) and you can make up your own different operators to do different things concisely. Reading a journal paper that uses dotted relational operators to mean something? Great! Use them in Haskell, too.
- It has infinite-precision integer arithmetic. It also has infinite-precision rational arithmetic, and fixed-precision types in case you want those, too. And you can build your own types somewhat concisely.
- It has an exponentiation operator. Actually, it has three! This is because there’s a difference in what the three of them mean in some cases, so you get your choice.
- It has powerful string handling. It does, but more importantly it also has powerful list handling, and these list handling routines are all usable on strings.
- It has few exceptions to its rules. Haskell’s semantics are basically the lambda calculus. The whole language is remarkably consistent and behaves in consistent ways. The semantics are even simpler and easier to understand than any imperative language, which has to do with the distinction between values and variables; or eager languages, which have to deal with the question of evaluation order. This makes Haskell programs very easy to understand and manipulate safely.
So there you have it.