Hello World Haskell

Teacher: Hello class! Welcome to your first day of functional programming. Today, we’re going to be talking about how to write the classic “Hello, World!” program in Haskell. It’ll be slightly more involved as we’ll ask for the user’s name and then greet them. I’m sure many of you have heard scary things about Haskell, but I promise you it’ll be fun. Student: I heard we have to learn about IO. That sounds scary! ...

September 16, 2022 · Drew Olson

Easy JSON in Haskell

So you’ve learned some basic Haskell and you’re feeling really good about yourself. You sit down the write some code and you’re presented with a deeply nested JSON structure: { "foo": "Hello", "bar": 1, "baz": "More stuff", "people": [ { "name": "Drew", "hobbies": [ { "name": "bridge" }, { "name": "haskell" } ] }, { "name": "Jane", "hobbies": [ { "name": "chess" }, { "name": "ocaml" } ] } ] } Your goal is to simply find the name of Drew’s first hobby. LET’S WRITE SOME TYPES! ...

September 12, 2022 · Drew Olson

Parsing Permutations

My favorite game is bridge. It’s an excellent test of cooperation and strategy. I’m in a discord chat devoted mostly to the game and folks often share interesting bridge hands with one another. I decided it would be fun to build a program that parsed a simply-formatted bridge hand and produced a plain text bridge diagram. Here’s a defensive problem that Sir Hugo Drax faced at Blades, defending a contract of 7♣xx. $ bridge-cli-exe <<< 't987 6543 - 76532 > akqj akqj ak kj9, r/r, imps, s6' Vul: R/R ♠T987 IMPs ♥6543 ♦ ♣76532 Lead: ♠6 ----- ♠AKQJ | N | ♥AKQJ | E| ♦AK | | ♣KJ9 ----- I’d like to focus on an interesting problem I faced when parsing this input. I wanted the user to be able to enter several elements: the layout (the cards), the vulnerability, the type of scoring, and the opening lead, each separated by a comma. However, I wanted these elements to be provided in any order. Furthermore, some of these elements were required while others were optional. ...

November 18, 2021 · Drew Olson

Adventures in Looping

I was recently building a Slack bot in Haskell. The core of the Slack integration was a never-ending loop that read messages from a web socket and performed actions based on the message. But how should I go about looping forever in Haskell? My first pass was to use the aptly-named forever function. My understanding of forever was that it ran a provided IO action over and over (this understanding was incomplete, we’ll get to that). My initial code looked vaguely like this: ...

September 29, 2021 · Drew Olson