I’ve spent the past several years working with functional programming languages in my free time – primarily Haskell and OCaml. I love both languages but also find aspects of each frustrating. Haskell is terse and elegant with type classes providing a powerful mechanism for ad-hoc polymorphism. However, it can also be confusingly implicit and I personally find lazy evaluation to have more downsides than upsides. OCaml is explicit and powerful with a best-in-class module system. However, I believe it is often exhaustingly explicit, especially when dealing with custom data types in generic containers. ...
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! ...
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! ...
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. ...
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: ...
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. One such pattern is called “applicative-style validation”, but I’ll simply call it “declarative validation”. In this post I’ll provide some motivation for using this technique and then build a small library in Python implementing these ideas. ...
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. I was surprised to discover how much Haskell I already knew from learning PureScript, but core features like laziness (PureScript is a strict language) took some getting used to. ...
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. ...
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. I decided to use SVG to draw the lines of the fractal. Initially, I built a solution in Elm. However, I was missing the flexibility I’d experienced with my previous back end work in PureScript and wanted to explore using it on the front end. This post walks through the process of rebuilding the solution in PureScript. ...
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? I know I have. You finally have the syntax correct and then new requirements get added. Suddenly you need to support optional tokens, lists of values and other complicated types of input. When regular expressions start to break down because of complexity, it’s time to reach for a more powerful abstraction. Enter parser combinators. ...