beanz Magazine

Haskell

Get lost in a mess of infinite lists with this kooky, lazy language.

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

Also In The December 2018 Issue

Create colourful geometric patterns in SketchUp inspired by the Great Mosque of Cordoba.

Learn how to use animated GIFs to create proper animation loops in Scratch.

Create an electronic obstacle course and test the steadiness of your hands as you navigate through.

What’s allowed in an e-mail address? Let’s break down the syntax.

Throwing your old tech in the trash is bad for the environment. Check out some safer, cleaner alternatives.

New, crafty mods that’ll push your Minecraft design skills to new levels.

Part philosophical paradox, part coding puzzle, quines are sure to tease and challenge your brain!

5 questions with the designer pushing innovation in 3D printed clothes.

All work and no play makes it harder for kids to learn, especially those with anxiety and learning disabilities.

Dive into the nuts & bolts of storing pictures and files on your computer.

A clever AI is creating twisted, surreal art. Math or magic?

Two doors hide a zonk, and one door hides a prize. Can probability & statistics help you pick the right one?

How can you tell if a credit card number is valid or invalid? Meet the Luhn algorithm, one of many checksums helping us keep the internet in order.

Learn about the Destination Dispatch algorithm and create your own robotic lift.

Grab your friends & disconnect from the digital world with these fun science & tech board games.

Interesting stories about computer science, software programming, and technology for December 2018.

Links from the bottom of all the December 2018 articles, collected in one place for you to print, share, or bookmark.