Book Image

Haskell Cookbook

Book Image

Haskell Cookbook

Overview of this book

Haskell is a purely functional language that has the great ability to develop large and difficult, but easily maintainable software. Haskell Cookbook provides recipes that start by illustrating the principles of functional programming in Haskell, and then gradually build up your expertise in creating industrial-strength programs to accomplish any goal. The book covers topics such as Functors, Applicatives, Monads, and Transformers. You will learn various ways to handle state in your application and explore advanced topics such as Generalized Algebraic Data Types, higher kind types, existential types, and type families. The book will discuss the association of lenses with type classes such as Functor, Foldable, and Traversable to help you manage deep data structures. With the help of the wide selection of examples in this book, you will be able to upgrade your Haskell programming skills and develop scalable software idiomatically.
Table of Contents (13 chapters)

Introduction

If you have worked with object-oriented programming, then you must be aware of the properties (such as in C# or Python, or even in managed C++). Usually, we can access the properties inside an object and also set the property to some value:

    Point point = Point(1.0, 2.0);
    double x = point.x; // Should be 1.0
    point.x = 3.0;      // Now point x is changed to 3.0

Though the preceding code mutates the data, it is very convenient to get and set a property. Imagine doing the same with Haskell:

    data Point = Point Double Double

    x :: Point -> Double
    x (Point xv _) = xv

    setx :: Point -> Double -> Point
    setx (Point _ y) x = Point x y

We need to de-construct a type, and reconstruct it again. If we had some generic way of accessing a field inside the data, and then accessing it back, then we will get the lost convenience of getting and...