What is Haskell notably good for?

Answer by Tikhon Jelvis:

Haskell is good for most things. So lets focus on the ones specific to web development.

Haskell offers quite a lot over other languages, even just for web development.

Concurrency

One place Haskell really shines is concurrency. It's fair to say that it's well ahead of pretty much every alternative here.

The base of the system are Haskell's green threads: these are lightweight threads managed by the language itself and mapped down to a smaller number of native OS threads. These threads are so lightweight that you can easily run millions on normal hardware. Millions! This gives you a very nice model for web programming: every request gets a thread. This leads to much simpler, easier to maintain logic, but you don't sacrifice performance: since the IO manager is intelligently implemented, your code ends up using an efficient event-loop even though it doesn't look event-based. Much like Node.js without any callbacks, except it's also intelligent enough to scale trivially over multiple cores. (And, I believe, faster.)

On top of this, the very design of Haskell makes for fewer concurrency bugs. There are exciting technologies like Software Transactional Memory (STM) which present a nice model for shared-memory concurrency without a large programming penalty. I've talked to some people who have a Haskell trading system that they converted to using STM; they reported a performance loss of ~4%. Given the benefits, this is a no-brainer!

STM, as well as the rest of Haskell's wonderful concurrency features are a very interesting topic but a bit too long for a single Quora answer. Happily, Simon Marlow (who is the most qualified person to talk about this) has written a very friendly book about this: Parallel and Concurrent Programming in Haskell. And, in the usual Haskell spirit, the book is available free online!

Suffice to say: Haskell will solve most of your concurrency problems for you. Without making compromises on your programming model like Node.js's callback hell. The code you write still looks mostly like normal, single-threaded Haskell; it just scales really well.

Safety

Another classical benefit of typed functional programming is safety. For web programming, you still get all the type safety you would with normal programs. But there are also ways to use the type system to prevent common web programming problems.

The Yesod Web Framework does a really good job of using Haskell's type system. The statically prevent:

  • XSS attacks: they ensure that user input is sanitized and prevent you from accidentally using unsanitized input
  • SQL injection: same idea with escaping SQL queries
  • other SQL errors: you query your database using normal Haskell constructs, which ensures SQL queries are well formed
  • missing template variables: even template variables are statically checked to make sure they exist and have the right type
  • broken links: they system ensures you never generate internal broken links
  • character encoding issues: the type system makes sure that you never accidentally mix up text encoded in different ways

More importantly, it does all this without excessive boilerplate. This is not Java! Haskell provides a bunch of advanced tools like global type inference and Template Haskell which lets you get away with as little keyboard typing as a dynamically typed language but with far more static typing :).

A side note about Template Haskell: just like the name implies, it's very good for, well, templates. This makes it very easy to embed HTML/CSS/JS templates in your code in intelligent ways—the results of templates are first-class citizens, which means you can pass them around, build functions from them and so on. It means that your inline HTML snippets use the same syntax and have the same functionality as your large full-page templates, with everything being really easy to combine.

Coincidentally, Yesod also has an O'Reilly book you can read. And it's also available free online: Developing Applications with Haskell and Yesod.

So don't worry about not having enough (free!) learning resources!

Expressiveness

People generally assume that, since Haskell is compiled, offers all these great features and has significant static guarantees, it has to be less expressive or more verbose than dynamically typed languages like Python. In my experience, this is not the case: Haskell is actually more expressive than many languages like Python.

Haskell has a few characteristics that make this possible:

  • type inference: since types are inferred globally, you almost never have to write them out unless it makes your code clearer
  • abstraction: Haskell has really good facilities for making high-level, reusable abstractions which save a lot of code in the long run.
  • flexibility: Haskell is flexible enough to admit good-looking DSLs for things like querying databases—you need far less boilerplate for these takss
  • libraries: Haskell has a surprising amount of general-purpose libraries that can be used in a whole bunch of contexts like lenses, which let you work with nested data types. (They're like "jQuery for data".)
  • functional programming: finally, functional programming is naturally more concise than imperative programming, especially with Haskell's more minimal syntax.
  • typeclasses: typeclasses allow you to write code that would not be possible in other languages. Combined with type inference, they get rid of a lot of redundant module/class names in other languages.

I've written a few (admittedly small) things in both Haskell and Python. Most of the time, the Haskell has fewer lines of code even though it's also statically typed and faster. What's not to like?

I think this is a very compelling set of capabilities for more effective web programming. It makes Haskell well worth learning! Remember that learning the language is an O(1) operation whereas the benefit you get is O(n) based on how much you use it!

Also, empirically, it's not nearly as hard to pick up as people make it out to be. One of my friends recently started using Haskell at IMVU—switching from PHP!—and they had no real problems getting the rest of the developers (who had not had any functional programming experience) working efficiently with Haskell. Both hiring and training aren't nearly the obstacles that you would imagine if you just read internet complaints about Haskell.

The engineer at IMVU responsible for bringing in Haskell has written a great blog post about it (What it's like to use Haskell); it's worth a read if you're considering Haskell as an option.

What is Haskell notably good for?

Advertisements

Leave a comment

Filed under Life

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s