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....

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....

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....

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)....

September 29, 2021 · Drew Olson

Declarative Validation

In the past two years I’ve become reasonably comfortable with both PureScript and Haskell. I’ve learned so many new things while diving into the pure functional programming ecosystem and many of these techniques can be applied to other paradigms. Unfortunately, the pure FP world can feel a bit like another dimension – where many programming problems have elegant solutions but the world of “regular” programming isn’t aware of these patterns....

April 27, 2021 · Drew Olson

PureScript and Haskell

Two years ago, I starting learning PureScript. I had been intrigued by purely functional programming for some time but had failed to learn Haskell once or twice. PureScript seemed to be a kinder, gentler introduction to this world while retaining the fundamental properties of pureness that made Haskell intriguing to me. As part of my learning process, I rebuilt a slack bot1 I had previously written in go. Once I had learned PureScript and become more comfortable with purely functional idioms, the next logical step seemed to be learning Haskell....

February 24, 2021 · Drew Olson

Parsing Untrusted Input with Elixir

I’ve spent a lot of time in the past year learning PureScript and it has drastically changed the way I think about programming in general. The biggest change in my thinking is described by the excellent blog post Parse, don’t validate. The most important passage in the post, I think, is this: Consider: what is a parser? Really, a parser is just a function that consumes less-structured input and produces more-structured output....

August 23, 2020 · Drew Olson

Drawing Fractals with PureScript

Recently, for no good reason, I added an easter egg to my personal website. I love fractals and decided to add a visualization of the dragon curve. Here were the requirements: Every 10 seconds, a new iteration of the dragon curve would be drawn. If the user clicked anywhere within the drawing area, a new iteration would be drawn and the 10 second clock would be reset. After the 10th iteration is drawn, the drawing should be cleared and the process should restart....

June 3, 2020 · Drew Olson

Parser Combinators in Elixir

Delving into the world of pure functional programming caused me to learn about parser combinators. Upon returning to Elixir, I was excited to see that nimble_parsec is a great example of a parser combinator library for the Elixir ecosystem. Parser combinators can be notoriously confusing when first learned. In this post I’ll provide a gentle introduction to parser combinators via nimble_parsec. What is a Parser Combinator? Have you ever found yourself writing a regular expression to parse input?...

March 14, 2020 · Drew Olson

Building a Slack Bot in PureScript

I recently open sourced my first large PureScript project. It’s a slack bot that allows searching for cards from the Epic Card Game. In this post, I’ll discuss the process of writing the application, what went well and what went poorly. An Overview of the Slack Bot I was excited about building this project in PureScript because it was complicated enough to be interesting but not so complicated as to be overwhelming....

January 31, 2020 · Drew Olson