The Fibonacci numbers are a well known recursive sequence, which is defined as followed
f[0] = 0
f[1] = 1
f[n] = f[n-1] + f[n-2]
The question is, how can we calculate them?
The first idea and probably most intuitive way is recursively. Why? Because the structure of the sequence itself is recursive, which means the implementation will be very similar to our definition.
I’ll chose JavaScript as the implementation language, simply because you can just open the developer console in your browser and paste in the snippets to see the results immediately…
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. We can look at the grammar definition for CSS 2.1 to figure out how…
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.
type Lens s a = forall f. Functor f => (a -> f a) -> s -> f s
What we’ll do here is further generalize it so that we can change the…
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:
We can describe a fold as taking a structure and reducing it to a
single result. That’s also why some languages have a reduce
function instead…
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.
data User = User deriving Show
findById :: Int -> IO (Maybe User)
findById 1 = return $ Just User
findById _ = return Nothing
findUsers :: Int -> Int -> IO (Maybe (UserHaskell 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:
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…
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 -> MessageIf 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. In most imperative languages those expectations are implicit and up to the programmer…
I’ve been a long time VIM user. I use it every day for all of my work and side projects, writing blog posts, writing other content, sometimes even for writing emails if the text is long enough. VIM is like my home and I’m deeply in love with it.
The problem is that VIM is a horrible IDE. It’s an amazing and super productive editor, but it really sucks at doing IDE-like things. Now you might be thinking I’m a noob who needs to click on good looking buttons in RubyMine to get things done. No, that’s not what I mean by IDE … let me explain.
Most…