Beginning Haskell: A Project-Based Approach
Alejandro Serrano Mena
Format: PDF / Kindle (mobi) / ePub
Beginning Haskell provides a broad-based introduction to the Haskell language, its libraries and environment, and to the functional programming paradigm that is fast growing in importance in the software industry. The book takes a project-based approach to learning the language that is unified around the building of a web-based storefront. Excellent coverage is given to the Haskell ecosystem and supporting tools. These include the Cabal build tool for managing projects and modules, the HUnit and QuickCheck tools for software testing, the Scotty framework for developing web applications, Persistent and Esqueleto for database access, and also parallel and distributed programming libraries.
Functional programming is gathering momentum, allowing programmers to express themselves in a more concise way, reducing boilerplate and increasing the safety of code. Indeed, mainstream languages such as C# and Java are adopting features from functional programming, and from languages implementing that paradigm. Haskell is an elegant and noise-free pure functional language with a long history, having a huge number of library contributors and an active community. This makes Haskell the best tool for both learning and applying functional programming, and Beginning Haskell the perfect book to show off the language and what it can do.
- Takes you through a series of projects showing the different parts of the language.
- Provides an overview of the most important libraries and tools in the Haskell ecosystem.
- Teaches you how to apply functional patterns in real-world scenarios.
how to add a test executable to your Cabal project. Inside a Cabal file, a test is defined in its own stanza, like library and executable parts were. Thus, the declaration of a test inside the project file is headed by test-suite followed by the name of the test suite. The key field to include in that stanza is the type one. Cabal has extensible support for different kinds of test providers that can run the tests in the project in different ways. The focus in this section will be on type being
value, namely, foldr1 and foldl1. In those, the starting value is the last element (in foldr1) or the first element (in foldl1) of the list. If you know any language derived from Lisp, such as Common Lisp, Racket, or Clojure, you will know this folding operation as reduce. It is not used much in Haskell, but it may come in handy in cases where handling the empty list case is guaranteed not to happen and where handling it tangles the code. As an example, the previously defined maximum function is
Usually, it also describes the simpler element you can get (just returning a value) for each kind of computation. At first, this might seem extremely vague, so let’s look at the implementation for your Maybe and State monads. The specific type for the return implementation of State s is a -> State s a or, equivalently, a -> s -> (a,s). This is the only implementation I can think of that returns the value that was passed, with the internal state unchanged. This is the same purpose of the remain
Search Problems Many problems in computer science can be better solved when looking at them as search problems. In a search problem you don’t know how to come to an answer directly, but you have a way to explore the space of possible solutions, and you can do so until you find the correct answer. Usually, the way to explore the space resembled the shape of a tree. From a possible solution you are able to create a set of other possible solutions to check. In this way, looking for the wanted value
-> Int -> Int -> [[Int]] paths edges start end = do (e_start, e_end) <- edges guard $ e_start == start subpath <- paths edges e_end end return $ start:subpath However, this code is not yet right because you need to manage the case in which you have reached the target year of your journey. In that case, you need to add to those paths an extra one consisting of just one year, the one that is at the same time start and end. To do so, you can use the (:) constructor to append the element