This book has been designed to be read cover-to-cover as a tutorial and workbook, or to be used in a classroom or reading group setting. Starting with Chapter 1, each new chapter will continue a theme or build on some knowledge that you picked up in the previous chapter. If you have some prior experience with Haskell, it’s worthwhile to start reading from the beginning so that you can follow along with the subsequent references to earlier material.
For more experienced Haskell developers, this book can also serve as a useful resource to help you learn some practical ways to apply more advanced techniques. If you’ve used Haskell in school or written a few small programs and are looking to move into building larger production applications, you may find it helpful to skim the first half of this book and then start reading the second half more thoroughly.
As you are working through the book, you’ll encounter several different kinds of example code. You should always be able to tell what type of environment you should be working in based on the formatting of the examples:
- Code that starts with a λ character should be typed into ghci .
- Lines that start with user@host$ should be typed into a shell like bash or zsh.
- Other code can be written in Haskell files using your text editor, or written directly into ghci at your discretion.
Until you have finished Chapter 5, create a new directory for each chapter. Inside of the directory you create for each chapter, create a file named after the current chapter, for example, Chapter1.hs . You can use this for keeping track of example code and experiments you want to run. You’ll also create several files named Main.hs as you are working through the examples. You can put each of these in a subdirectory, for example, one subdirectory per chapter, or you can rename your old Main.hs files when you are no longer actively working through them. Whatever organizational scheme you prefer, ensure you keep around all your examples and experiments since you’ll want to refer to them frequently as you are learning.
Once you’ve worked through the chapter on Cabal you’ll be better equipped to create fully stand-alone projects that you can build. You’ll also learn how to re-use code that you’ve written. From that point onward, you can create a new project for each chapter or each major example.
Following Along with Example Code
As you read this book, you’ll work through examples iteratively, making changes to earlier code and adding new features. Once you’ve learned about how to import code from other modules, we’ll begin introducing new features iteratively that require adding additional imports. Similarly, once you’ve learned how to work with language extensions, we’ll add them as we work through examples. The rest of this section will discuss how we’ll approach introducing new imports and extensions in example code. Don’t worry too much about the syntax yet. As you work through the book, you’ll learn about imports and language extensions before they are required for any examples. For now, skim this section and feel free to come back to it later if you need to.
Most of the chapters in this book will focus on building up a few small example programs. In some cases, we’ll explicitly define a new module when we’re starting a new example. In this case, these new modules may start out including a few language extensions or imports.
| | {-# LANGUAGE TypeApplications #-} |
| | {-# LANGUAGE DerivingStrategies #-} |
| | module Main Where |
| | |
| | import Data.Text ( Text ) |
| | |
| | main :: IO () |
| | main = print helloWorld |
As we iterate through the example, we’ll add new features that might require additional language extensions or add-ons. When we’re getting ready to use a new module or extension, we’ll add them to the top of an example:
| | {-# LANGUAGE OverloadedStrings #-} |
| | import Data.ByteString ( ByteString ) |
| | |
| | helloWorld :: ByteString |
| | helloWorld = "Hello, World" |
In your own code, you should add these to the relevant parts of your module. Here’s an example of what your own code should look like as you follow along with the examples:
| | {-# LANGUAGE TypeApplications #-} |
| | {-# LANGUAGE DerivingStrategies #-} |
| | {-# LANGUAGE OverloadedStrings #-} |
| | module Main Where |
| | |
| | import Data.ByteString ( ByteString ) |
| | import Data.Text ( Text ) |
| | |
| | helloWorld :: ByteString |
| | helloWorld = "Hello, World" |
| | |
| | main :: IO () |
| | main = print helloWorld |
Not all of the examples that you work through will start with a module and a set of imports or extensions. In these cases, you can start with your own empty module, or you can work though the examples in ghci .
Compiler Versions, Language Standards, and Extensions
Although there have been several different implementations of Haskell over the years, the Glasgow Haskell Compiler (GHC) is the de facto standard Haskell compiler. In this book we’ll focus on Haskell as implemented by GHC 9.4, which is the newest stable release at the time of this writing. All of the examples have also been tested with GHC 8.10.
| Compiler Version Differences |
|---|
|
| A few examples in this book will use newer features of GHC not available in version 8.10. Look out for an aside, like this one, to learn about newer features and how to write code without those features when you need to support older compilers. |
As Haskell evolves, new features are typically added through extensions . Language extensions allow you to enable and disable specific language features. The Haskell2010 language standard is the default language version that’s used by GHC 8.10, and it includes a number of extensions that are enabled by default. In GHC 9.4, the GHC2021 language version is used by default. GHC2021 isn’t an officially published Haskell standard; instead it represents a number of commonly accepted GHC specific nonstandard extensions to Haskell2010 that are enabled by default.
In this book, we’ll target Haskell2010. Any language extensions that aren’t included in Haskell2010 will be introduced and discussed. Complete example programs will always include all extensions that would be required when using Haskell2010. Shorter examples may omit language extensions for the sake of readability.
| GHC2021 Extensions |
|---|
|
| We’ll target Haskell2010 as a baseline when choosing which language extensions to highlight in this book. If you’re using GHC2021, look for an aside like this to tell you when an extension is included by default and doesn’t need to be enabled explicitly. |
| Extension | Enabled |
|---|---|
| AllowAmbiguousTypes | Manually |
| BangPatterns | GHC2021 |
| ConstraintKinds | GHC2021 |
| DataKinds | Manually |
| DefaultSignatures | Manually |
| DeriveAnyClass | Manually |
| DerivingStrategies | Manually |
| DerivingVia | Manually |
| ExistentialQuantification | GHC2021 |
| ExplicitForAll | GHC2021 |
| FlexibleContexts | GHC2021 |
| FlexibleInstances | GHC2021 |
| FunctionalDependencies | Manually |
| GADTs | Manually |
| GeneralizedNewtypeDeriving | GHC2021 |
| KindSignatures | GHC2021 |
| MultiParamTypeClasses | GHC2021 |
| OverloadedStrings | Manually |
| PolyKinds | GHC2021 |
| QuantifiedConstraints | Manually |
| RankNTypes | GHC2021 |
| RecordWildCards | Manually |
| ScopedTypeVariables | GHC2021 |
| TupleSections | GHC2021 |
| TypeApplications | GHC2021 |
| TypeFamilies | Manually |
| TypeOperators | GHC2021 |
| UndecidableInstances | Manually |
| NoStarIsType | Manually |
| PolyKinds | GHC2021 |
| StandaloneDeriving | GHC2021 |
Libraries and Library Versions
The examples in this book stick to the standard library, base , as much as possible. For features that aren’t available in base , we’ll stick to a small selection of popular libraries. This should ensure maximum compatibility, at the cost of not showing off some very interesting libraries that are worth learning. The following table includes the exact versions of each package that were used for the examples. In any cases where there are incompatible changes between library versions, we’ll use the most recent version of the library.
| Package | Version (GHC 9.4) | Version (GHC 8.10) |
|---|---|---|
| base | 4.17.0.0 | 4.14.3.0 |
| bytestring | 0.11.3.1 | 0.10.12.0 |
| base64-bytestring | 1.2.1.0 | 1.2.1.0 |
| text | 2.0.1 | 1.2.4.1 |
| containers | 0.6.6 | 0.6.5.1 |
| vector | 0.12.3.1 | 0.12.3.1 |
| time | 1.12.2 | 1.9.3 |
| unix | 2.7.3 | 2.7.2.2 |
| mtl | 2.2.2 | 2.2.2 |
| transformers | 0.5.6.2 | 0.5.6.2 |
| process | 1.6.15.0 | 1.6.13.2 |
Copyright © 2023, The Pragmatic Bookshelf.