Parsing CSS with Parsec

This article is a small introduction to Parsec, the Haskell parser combinator library for Haskell. We’ll use it to parse simple CSS file such as the following. .container h1 { color: rgba(255, 0, 0, 0.9); font-size: 24px; } First we need to figure out our data structure which will represent the syntax tree. Since this is just an introduction, we’ll go easy and ignore features like media queries. In order to create the structure we need to figure out how to name things.

Read More →

Lens Tutorial - Stab & Traversal (Part 2)

In the first article in the series about lenses, we’ve looked at the motivation behind the lens library, and we also derived the basic type of Lens s a. In this article we’ll go deeper and explain the reasoning beheind the more generic Lens s t a b type. We’ll also take a look at how we can get a multi focus lens using a Traversal. Just to reiterate, here’s how looks the type we derived in the previous article.

Read More →

Foldable and Traversable

Before we can get into the more advanced topics on Lenses, it is important to really understand both Foldable and Traversable, which is the motivation behind this article. Let’s begin with Foldable. Foldable represents structures which can be folded. What does that mean? Here are a few examples: Calculating the sum of a list. Calculating the product of a list. Folding a tree to get a maximum value. We can describe a fold as taking a structure and reducing it to a single result.

Read More →

Building Monad Transformers - Part 1

In this article we'll focus on building our own monad transformers. We'll start out with an example code and improve it by building a simple wrapper over IO (Maybe a). The following example is really simple, but I'm sure you can imagine doing something similar in your own application. The findById method is there just to simulate a database query that might not find a result.

Read More →

Mutable State in Haskell

Haskell is a purely functional language, which means there are no side-effects and all variables are immutable. But as you probably know this isn’t completely true. All variables are indeed immutable, but there are ways to construct mutable references where we can change what the reference points to. Without side effects we wouldn’t be able to do much, which is why Haskell gives us the IO monad. In a similar manner we have many ways to achieve mutable state in Haskell, let’s take a look at them:

Read More →

Lens Tutorial - Introduction (part 1)

This article is the first in the upcoming series that aims to explain the Haskell’s lens library and the ideas behind it in an approachable way. Don’t worry if you’re new to Haskell, the only prerequisites here should be understanding of the Functor type class, and understanding how records and algebraic data types work in Haskell. We won’t be using the lens library in this article yet. The API we’ll develop will be exactly the same, but for the sake of learning I’ll try to show you how everything works and why it works by re-implementing it from scratch.

Read More →

Using Phantom Types in Haskell for Extra Safety - Part 2

I’ve received a lot of reactions to the previous blog post about Phantom Types over the past two days, which is why I’ve decided to summarize what I’ve learned in another blog post. First, here’s a summarized problem from the previous post. We have a Message which can be either PlainText or Encrypted. We’ve used Phantom Types to enforce this in the type system: data Message a = Message String data PlainText data Encrypted send :: Message Encrypted -> IO () encrypt :: Message PlainText -> Message Encrypted decrypt :: Message Encrypted -> Message PlainText Can newtype do the same?

Read More →

Using Phantom Types for Extra Safety

If you’ve been programming in a dynamic language, you’ve probably heard that type systems can catch more errors before your application even gets run. The more powerful the type system is, the more you can express in it. And because we’re talking about Haskell, we have a great number of tools at our disposal when trying to express things in terms of the types. Why is this important? Sometimes a function has an expectation about the value that it’s receiving.

Read More →

Yesod is Fun

I’ve been trying many Haskell web frameworks over the past few weeks. I wrote one small app with Simple, almost wrote another one with Scotty. Then decided it’s time to take a look at the big guys, Happstack, Snap and Yesod. First I tried Happstack, which felt kind of OK and very understandable, mostly because it doesn’t seem to be trying to do much magic. This is really great for learning, but then I stumbled when I found that it’s not actually being developed on GitHub.

Read More →