An excerpt from Haskell Bookcamp by Phillip Hagenlocher
If you’re interested in learning Haskell and are looking for a place to start, this is the book for you.
Haskell has a reputation for being overly academic and hard to learn, but the truth is that it’s not as much of a bear as people think. You just need the right guide.
Take 25% off Haskell Bookcamp by entering fcchagenlocher into the discount code box at manning.com.
Complex software is all around us and we programmers need tools in order to construct it. Mainly, we need programming languages that can facilitate and ease our development process and Haskell, a state-of-the-art language providing a mix of various cutting edge technologies, is exactly that. Featuring an impressive amount of language features mixed with a certain elegance that few other languages can achieve, Haskell has become shrouded in legend and myth…and we are going to take a look behind the curtain!
In this book we will cover the implementation of various small (some might even call them tiny) projects. Some of them are just for fun, some of them are useful tools and some of them were chosen to specifically show you, the reader, how to effectively use Haskell to create fast, safe and reliable software. In following along with these projects, you will learn the ins and outs of writing software using some of the most elegant and elaborate programming models.
If you have researched Haskell before and thought “I don’t know where to start” or were unsure about the practicality of it, don’t worry. This book does not require any prior knowledge about any of Haskell’s features or functional programming in general. We will start slow with simple algorithms and end by writing our own web servers!
So, jump in. The journey is worth it!
What is Haskell?
If you have picked up this book, there is a good chance you already have some idea of what Haskell is. You know that it is a programming language, you most likely know that it is a functional programming language and you probably have heard of the unique nature of some of its features. But why would you use Haskell and what is it that makes it special?
The first exciting thing are the many high-level abstractions such as algebraic data types, type classes and monads, all of which will be explored in this book. These abstractions let us write neat, composable and clean code that let us generalize complexities into reusable functionalities. Why is that a good thing? It saves time and avoids a lot of headaches when debugging. It is very common to implement algorithms only once in Haskell and then reuse them in different contexts over and over again. Why write a sorting algorithm for multiple different data types, when you could just write it once?
The second special property of Haskell is its direct connection to academia and contemporary programming language research. Other languages typically are born from an industrial need. This reflects heavily in their architectural design. Java serves as a prime example since many low level design decisions were first made due to the advertised write once, run anywhere promise. Later specification changes came with the Enterprise Edition, promising a well-designed API for web services and distributed computing. Haskell on the other hand doesn’t have such a background. It was built with (then modern) results from type theory and programming language design focusing on simple concepts from which the language was built. The core language is thus relatively simple and small. Many of the additional features of the language where first studied in academia and then added to it. This makes Haskell much more focused on program correctness than other languages leading to design decisions that heavily favor safety.
However, that does not mean that performance has been neglected in Haskell. Its compiler is an optimizing compiler which means that it will try to analyze the source code and perform rewriting steps to decrease its execution time. In certain situations, aggressive optimizations can even lead to code with a performance comparable to low-level languages such as C! Haskell also features powerful libraries for parallelism, concurrency and asynchronous computations, making it simple to write multi-threaded code to improve performance even further, all the while you as a programmer do not have to worry about pesky details such as memory-management since the run time uses a garbage collector.
Haskell is not an ordinary functional programming language but it is a pure functional programming language. By pure we mean that functions we write in our programs are much like functions in the mathematical sense. They have input and produce some output, but have no side effects. This means that functions can only work with the data we put into them and cannot, for example, read additional data from files or the network. Is that helpful? Yes! It makes Haskell programs easier to analyze and to understand just by reading the code itself and generally leads to less bugs and more reliable software. We will explore this concept at great length in this book since it forces us to think about programming differently than we normally do.
Who is this book for?
Some people claim you have to have studied maths in order to learn Haskell. This book tries to once and for all break this stereotype by explaining the fundamental concepts of the language in an easy to understand manner. You don’t have to any prior exposure to functional programming at all! However, you should be familiar with procedural or imperative programming (C, Java, Python, etc.) and have a base understanding of algorithms and data structures as well as operating and file systems. The ideal reader has:
- At least 1-2 years of programming experience (not necessarily with Haskell, don’t worry)
- Worked on different (small) software projects and knows the problems that can arise in real-world applications
- Some basic knowledge on operating systems (specifically UNIX)
What you will learn from this book
As opposed to similar learning materials, this book will not give you a crash course on the most advanced techniques and concepts in Haskell, rather it will highlight fun, creative and useful projects, showing you how to tackle certain problems that arise when writing real applications. The chapters will also present best practices and explain why these practices are important. Haskell can be an ocean of foreign concepts and this book is not trying to give you a deep dive. It rather tries to be the diving instructor showing you the safer, shallower depths of this ocean. By the end of the last chapter you should then feel comfortable to board a submarine and explore the deepest depths yourself. It’s going to be an amazing journey.
This book is designed to cover a large variety of projects. We will begin by writing beginner friendly tools like a simple (but clever) artificial intelligence for a special variety of the game word ladder and a CSV tool, which will be capable of neatly formatting a CSV file and printing it as a table in ASCII form as well as provide additional features like searching. Later in the book we will cover more data intensive work like working with audio and image files and how to manipulate them within our software, even creating our own musical synthesizer and multi-threaded image processing library. Of course no book on Haskell would be complete without trying to emulate the big boys by creating our own microservice! A web server capable of answering requests by performing actions that we can configure on the spot. All the while we will have a chance to look at some advanced Haskell libraries in action!
We will mostly focus on writing applications used on UNIX-like systems like Linux, BSD or Mac OS. If you want to use Haskell on Windows that is definitely possible by using WSL (Windows Subsystem for Linux). While Haskell can also be used natively on Windows I personally would not recommend it. Most of the projects will consist of applications running on the command line or terminal. This is a deliberate choice since it simplifies the development process when we can focus and concentrate on minimal user interfaces and don’t have to worry about GUI programming.
After reading this book readers will feel comfortable in the usage with Haskell, implementing real-world projects, not falling into the typical pitfalls that beginners usually fall into. Additionally, readers will know how to apply the learned functional concepts universally to other languages.
You can learn more about the book here.