dark mode light mode Search Menu
Search

Haskell

Last time we talked about the oldest thing you could even call a programming language, but now we’re going to talk about one of it’s most interesting descendents: Haskell!

Haskell is kind of a language for programming-language-design nerds. It was originally developed in the ’90s as a test-bed by language design researchers, but now it’s a mainstream programming language. Today it’s used for all sorts of applications and website building in addition to its more highbrow role in compiler writing, developing projects for the Department of Defense, or complex analysis software for large banks and the financial sector. I think one of the cooler Haskell projects I’ve come across lately is a music generation system called Tidal, which was used in the development of the No Man’s Sky soundtrack.

Haskell is a language with a lot of interesting features, some of them still unique after over 20 years! I’m going to focus on one of the weirder features about Haskell: programming with infinite loops.

First, I’ll say that the best way to get started with Haskell is The Haskell Platform. It comes with the main Haskell compiler, GHC (The Glorious Glasgow Haskell Compiler), as well as a number of libraries and tools that will be useful for getting started as a Haskell programmer. You can run the GHCi executable to get a nice little interactive command line just like you’ve seen in Ruby, Python, and a bunch of other languages.

Now, we’re going to show a really simple example of Haskell code:

messOfTwos = 2:messOfTwos
main = print (take 10 messOfTwos)

Let’s break down the syntax quickly: messOfTwos is a definition of a list. Lists in Haskell are pretty standard for most programming languages and are built out of two operators, : (pronounced “cons”) and [] (pronounced nil). You can either build a list with these operators or with a convenient syntax like [1,2,3]. This is the same as the list 1:2:3:[] built step-by-step with cons.

The second line of this program says that main, the function that runs when you execute the whole program, is going to print out the first ten elements of the list messOfTwos.

We’re ready to talk about what’s so weird about this code: we can do this by looking at the definition of messOfTwos = 2:messOfTwos. We can read this aloud as “messOfTwos is a 2 followed by messOfTwos. In Haskell, we can treat the equals sign like a real equals in mathematics—which means that we can substitute in the definition of the left-hand side into the right-hand side.

So messOfTwos = 2:messOfTwos can be unrolled into messOfTwos = 2:2:messOfTwos and then further into 2:2:2:messOfTwos and arbitrarily far into something like 2:2:2:2:2:2:2:2:2:2:messOfTwos. I think it’s clear at this point that messOfTwos is an infinite list of twos. So why doesn’t this infinite list cause Haskell to hiccup and go into an infinite loop trying to print the first ten elements?

Because Haskell is a lazy language. That’s not an insult! It’s a style of computation! A lazy language leaves all its definitions rolled-up, uncomputed, until they’re needed. Then it computes just enough to finish the program. That process of unrolling the definition that we did by hand up above? That’s actually what Haskell programs do when they run! Since the program needs to know the first ten elements of the list messOfTwos then it only unrolls until it knows what the first ten elements are and prints them out!

Hopefully this gives a little teeny taste of the fact that Haskell is unusual and interesting. We haven’t even gotten into some of the other interesting features like:

  • A type-system you don’t have to type (ehhhh?) because it can infer what kinds of data you’re using without being told
  • A really cool way of handling blocks of code as first-class objects you can pass around to functions, letting you write your own control structures like loops and conditionals
  • Code that’s easy to prove properties about and calculate by hand before you even run your program

Haskell is actually one of my favorite languages, one that I’ve been using off and on for over a decade. I hope you check it out!

Learn More

A simple introduction to Haskell

http://learnyouahaskell.com/chapters

Another good (but less meme-y) introduction to Haskell

http://book.realworldhaskell.org/

A nice summary of a bunch of the ways that Haskell is used

https://www.quora.com/Where-is-Haskell-used-in-industry-today-2015

The big central repository of community Haskell libraries

https://hackage.haskell.org/

A cool music generation system written in Haskell

https://tidalcycles.org/
This is used by the band 65daysofstatic, who made the soundtrack of No Man’s Sky