E-Book, Englisch, 418 Seiten
Schrijvers Soar with Haskell
1. Auflage 2023
ISBN: 978-1-80512-256-2
Verlag: Packt Publishing
Format: EPUB
Kopierschutz: 0 - No protection
The ultimate beginners' guide to mastering functional programming from the ground up
E-Book, Englisch, 418 Seiten
ISBN: 978-1-80512-256-2
Verlag: Packt Publishing
Format: EPUB
Kopierschutz: 0 - No protection
With software systems reaching new levels of complexity and programmers aiming for the highest productivity levels, software developers and language designers are turning toward functional programming because of its powerful and mature abstraction mechanisms. This book will help you tap into this approach with Haskell, the programming language that has been leading the way in pure functional programming for over three decades.
The book begins by helping you get to grips with basic functions and algebraic datatypes, and gradually adds abstraction mechanisms and other powerful language features. Next, you'll explore recursion, formulate higher-order functions as reusable templates, and get the job done with laziness. As you advance, you'll learn how Haskell reconciliates its purity with the practical need for side effects and comes out stronger with a rich hierarchy of abstractions, such as functors, applicative functors, and monads. Finally, you'll understand how all these elements are combined in the design and implementation of custom domain-specific languages for tackling practical problems such as parsing, as well as the revolutionary functional technique of property-based testing.
By the end of this book, you'll have mastered the key concepts of functional programming and be able to develop idiomatic Haskell solutions.
Autoren/Hrsg.
Weitere Infos & Material
Preface
- Functional Programming (FP) is one of the main programming paradigms, along with imperative programming and object-oriented programming. It uses functions as its core concept of computation – turning input into output in a predictable and context-independent way.While many non-FP languages also offer functions in some form, in FP, language functions truly have first-class status. They are not only computation but also data, which can be ferried around by other (higher-order) functions, dynamically assembled out of simpler functions, stored in data structures, or data containers themselves.
- Haskell stands out among FP languages in that it unequivocally embraces the FP paradigm. Because it does not make any compromises for imperative programming, Haskell had to come up with entirely new solutions to tackle common programming problems that also turned out to be successful in solving next-level problems. This way, it has become an inspiration for the designers of other programming languages (both FP and non-FP) and libraries in those languages.
Besides being true to the principles of FP, Haskell is also renowned for its sophisticated static type system. This means that Haskell programs are automatically checked for particular kinds of mistakes (known as type errors) before they are run. Moreover, thanks to Haskell’s powerful type inference mechanism, programmers have to write little to no type annotations themselves. Taking all the preceding aspects of FP in Haskell into consideration, the common theme of this book is abstraction. It provides many mechanisms for abstraction and powerful examples of abstractions that allow us to converse and reason about common programming patterns, becoming more effective programmers when we (re)use them.
Who this book is for
This book is for all those who already have some programming experience and want to learn FP in Haskell:
- If you are familiar with imperative or object-oriented programming, this book introduces you to the wonderful world of FP.
- If you are already familiar with other FP languages, you will discover the unique Haskell language features, ideas, and programming style.
- If you have outgrown your current programming language, are ready for a new challenge, or want to fall in love with programming all over again, your journey starts here.
What this book covers
, , explains the core concept of FP – functions. It introduces function definitions and shows how functions are called. This includes an explanation of all the syntactic elements (types, type signature, function body, etc) and their role. Along the way, it also introduces a number of built-in types and functions.
, , introduces Haskell’s mechanism for user-defined types – Algebraic Datatypes (ADTs). We build up from simple forms of ADTs such as enumerations and records to their full generality, and we learn the different elements of ADT definitions – the type name, the data constructors, and their fields. We see how ADT values are created and how they are taken apart by pattern matching. Finally, we see how ADTs can be parameterized over other types.
, , presents the functional programming alternative to loops. Recursive datatypes are a natural mechanism to express data structures of arbitrary size, and recursive functions are the way to process these functions. We will focus in particular on structural recursion as a principled use of recursion but also cover alternative recursion patterns.
, , explains how repeated patterns in function definitions can be abstracted over, notably by abstracting over higher-order parameters. Special attention goes to commonly used higher-order library functions such as map, filter, foldr, and foldl.
, , covers a range of language features and mechanisms that facilitate function-oriented programming, where we program at the level of functions rather than plain values.
, , presents Haskell’s unique mechanism for supporting ad-hoc overloading–type classes. It explains what ad-hoc overloading is and how functions give rise to polymorphic type signatures with type class constraints. Then, we will see how predefined type classes can be instantiated for user-defined types and how new user-defined type classes can be created. Finally, we cover several user-defined type classes and their use in standard libraries.
, , reveals Haskell’s unique evaluation mechanism – lazy evaluation. It shows how lazy evaluation works and improves upon both call-by-value and call-by-name. Then, it shows how to use the strategy to your advantage by using lists as iterators that supply their elements on demand. Finally, the chapter also points out a pitfall of the mechanism – the build-up of thunks.
, , explains Haskell’s unique way of interfacing with the outside world – its I/O mechanism. First, the chapter explains why the existing approach of other languages is problematic due to the lazy evaluation strategy and the language’s purity principle. Then, it presents the Haskell solution using the I/O type and >>=/return operators. Then, it introduces the do notation as a more user-friendly notation for I/O steps. Finally, it covers common I/O operations.
, , introduces the notion of foldables. These are collections that support a similar range of frequently used operations. Along the way, we learn that Haskell has a mechanism to abstract over parts of types, called type constructors, and uses algebraic concepts such as semigroups and monoids to design highly general algorithms.
, , leads us further into the hierarchy of type classes for type constructors. We will first consider functors – data structures that can be mapped over. Then, we will move on to applicative functors, which can merge multiple data structures, and finally, we will see traversables as data structures that can be mapped over with side effects.
, , introduces the king of the type constructor hierarchy – monads. We will first cover two well-known examples of monads – the Maybe monad for failure and the state monad for state passing. Then, we will generalize from these examples and present the Monad type class. Finally, we will present a number of additional examples of monads.
, , shows how the functionality of different monads can be combined into a single monad. The chapter covers the monad transformer mechanism and gives a range of commonly used instances. Then, it shows how applications can abstract over the implementation details of monads and monad transformers, with monad subclasses. Finally, we will see how the same monad transformers can be combined in different ways to achieve different behavior.
, , documents a powerful problem-solving technique that is appropriate when a range of problems have to be solved within the same problem area – domain-specific languages (DSLs) embedded in Haskell. First, we will see several examples of DSLs that are tailored to different problem areas. Then, we will focus on implementation techniques for DSLs. The most general and flexible approach is deep embedding. It contrasts with shallow embedding, which is a more lightweight and efficient technique.
, , introduces parser combinators, a lightweight and convenient DSL in Haskell for parsing. It covers what parsing is and where it is used. Then, we will see a basic definition of parser combinators to get a good idea of how they work. From there, we will move on to the industrial-strength Parsec library. Finally, we see how to tackle common parsing problems with parser combinators.
, , presents an exciting approach to the mundane activity of data access in nested datatypes. First, it demonstrates the basic approach for records that is built into Haskell and identifies its disadvantages. Then, it presents the concept of lenses as a much more convenient alternative for both reading and updating fields. We will show that lenses not only compose trivially to reach deep into data structures but also can be used to seamlessly define virtual fields. Finally, we cover the main lens combinators provided by the well-known...