Beginning Haskell: A Project-Based Approach

Book description

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.

  • What you'll learn

  • Build small and large projects using the Haskell language.

  • Profit from ecosystem tools such as Cabal, HLint, and QuickCheck.

  • Implement efficient stream I/O via libraries like Conduit.

  • Parallelize code to run on multiple processors or distributed across a network.

  • Create domain-specific languages useable by business users, and to tackle specific problem domains.

  • Build Haskell-backed websites using database and web-application libraries such as Persistent, Esqueleto, and Scotty

  • Who this book is for

    Beginning Haskell is for programmers new to functional programming, who want to learn this new paradigm and how it can improve the quality of their code. Beginning Haskell is also a great choice for functional programmers wanting to get a taste of the Haskell ecosystem and its unique features, or who wish to learn about advanced type system features and patterns.

    Table of contents

    1. Cover
    2. Title
    3. Copyright
    4. Dedication
    5. Contents at a Glance
    6. Contents
    7. About the Author
    8. About the Technical Reviewer
    9. Acknowledgments
    10. Introduction
    11. Part 1: First Steps
      1. Chapter 1: Going Functional
        1. Why Haskell?
          1. Why Pure Functional Programming?
          2. Why Strong Static Typing?
          3. The Haskell Ecosystem
        2. The History of Haskell
        3. Your Working Environment
          1. Installing on Windows
          2. Installing on Mac OS X
          3. Installing on Linux
          4. Installing on Linux from Source
          5. Checking That the Installation Is Successful
          6. Installing EclipseFP
        4. Taking Your First Steps with GHCi
        5. The Time Machine Store
        6. Summary
      2. Chapter 2: Declaring the Data Model
        1. Working with Characters, Numbers, and Lists
          1. Characters
          2. Numbers
          3. Strings
          4. Lists
          5. List Operations
        2. Creating a New Project
          1. Creating a Project from the Command Line
          2. Creating a Project from EclipseFP
          3. Understanding Modules
        3. Defining Simple Functions
          1. Creating a Simple Function
          2. Specifying the Function’s Type
          3. Developing a Robust Example
          4. Returning More Than One Value
        4. Working with Data Types
        5. Pattern Matching
          1. Simple Patterns
          2. Lists and Tuples
          3. Guards
          4. View Patterns
        6. Records
          1. Creation and Use
          2. The Default Values Idiom
        7. Summary
      3. Chapter 3: Reusing Code Through Lists
        1. Parametric Polymorphism
        2. Functions As Parameters
          1. Higher-Order Functions
          2. Anonymous Functions
          3. Partial Application of a Function
        3. More on Modules
          1. Module Imports
          2. Smart Constructors and Views
        4. Diving into Lists
          1. Folds
          2. Lists and Predicates
          3. Lists Containing Tuples
        5. List Comprehensions
        6. Haskell Origami
        7. Summary
      4. Chapter 4: Using Containers and Type Classes
        1. Using Packages
          1. Managing Packages with Cabal and EclipseFP
          2. Sandboxed Environments
        2. Containers: Maps, Sets, Trees, Graphs
          1. Maps
          2. Sets
          3. Trees
          4. Graphs
          5. Obtaining Help
        3. Ad Hoc Polymorphism: Type Classes
          1. Declaring Classes and Instances
          2. Built-in Type Classes
        4. Binary Tress for the Minimum Price
          1. Step 1: Simple Binary Trees
          2. Step 2: Polymorphic Binary Trees
          3. Step 3: Binary Trees with Monoidal Cache
        5. Container-Related Type Classes
          1. Functors
          2. Foldables
        6. Summary
      5. Chapter 5: Laziness and Infinite Structures
        1. An Infinite Number of Time Machines
        2. Lazy Evaluation Model
          1. Understanding Evaluation in Haskell
          2. Problems with Laziness
          3. Pattern Matching and Laziness
        3. Profiling with GHC
        4. Strictness Annotations
        5. Summary
    12. Part 2: Data Mining
      1. Chapter 6: Knowing Your Clients Using Monads
        1. Data Mining
          1. Implementing K-means
          2. Lenses
        2. Discovering Monads
          1. Watching Out for Incomplete Data
          2. Combinators for State
          3. Dissecting the Combinators
          4. do Notation
          5. Monad Laws
        3. Different Sorts of State
          1. State and Lenses
          2. Reader, Writer, and RWS
          3. Mutable References with ST
        4. Summary
      2. Chapter 7: More Monads: Now for Recommendations
        1. Returning More Than One Value
          1. The List Monad
          2. A New View Over Monads
        2. Failures and Alternatives
        3. Association Rules Learning
          1. Flattening Values into Transactions
          2. The Apriori Algorithm
        4. Search Problems
          1. Paths in a Graph
          2. The Logic Monad
        5. Monads and Lists Redux
          1. Combining Values Under a Monad
          2. Monad Comprehensions
        6. Combining Monads
          1. Monad Transformers
          2. Monad Classes
        7. Summary
      3. Chapter 8: Working in Several Cores
        1. Parallelism, Concurrency, and Distribution
        2. The Par Monad
          1. Futures
          2. Dataflow Parallelism with IVars
          3. Parallelizing the Apriori Algorithm
        3. Software Transactional Memory
          1. Concurrent Use of Resources
          2. Atomic Transactions
          3. Rolling Back Transactions
          4. Producer-Consumer Queues
        4. Cloud Haskell
          1. Looking for Galaxies
          2. Looking for Typed Galaxies
          3. Extra Features
        5. Summary
    13. Part 3: Resource Handling
      1. Chapter 9: Dealing with Files: IO and Conduit
        1. Basic Input and Output
        2. Randomness
        3. Working with Files
          1. Reading and Writing
          2. Handling Files
        4. Error Handling
          1. Pure Errors
          2. Catching Exceptions
          3. Throwing Exceptions
        5. Streaming Data with Conduit
          1. Problems with Lazy Input/Output
          2. Introducing Conduits
          3. Accessing Files via Conduit
        6. Looking Further Than Text Files
          1. Basic Networking
          2. Binary Serialization
        7. Summary
      2. Chapter 10: Building and Parsing Text
        1. The Five Textual Data Types
        2. Building as Fast as the Wind
        3. Parsing with attoparsec
        4. Introducing New Type Classes
          1. Applicative
          2. Functors, Applicatives, and Monads
          3. Alternative
          4. Traversable
        5. Don’t Overengineer: Just Use JSON
        6. Summary
      3. Chapter 11: Safe Database Access
        1. Database Access Landscape
          1. Abstracting Over Several DBMSs
          2. Introducing Persistent and Esqueleto
        2. Connection
        3. Schemas and Migrations
          1. Describing the Entities
          2. Creating the Database
        4. Queries
          1. Queries by Identifier or Uniqueness
          2. Selecting Several Entities
          3. SQL Queries with Esqueleto
        5. Insertions, Updates, and Deletions
        6. Summary
      4. Chapter 12: Web Applications
        1. Haskell Web Ecosystem
          1. Web Frameworks
          2. Compilation to JavaScript
        2. RESTful Structure
        3. Back End with Scotty
          1. Simple Skeleton
          2. Showing Products from the Database
          3. Inserting New Products Using Forms
        4. Front End with Fay
          1. Foreign Function Interface
          2. Fay and jQuery
        5. Summary
    14. Part 4: Domain Specific Languages
      1. Chapter 13: Strong Types for Describing Offers
        1. Domain-Specific Languages
          1. Embedding Your Language in Haskell
          2. The Offers Language
        2. Adding Safety to the Expression Language
        3. Dependent Typing
          1. Introducing Idris
          2. Enforcing the Presents Rule in Idris
        4. Type-Level Programming in Haskell
          1. Two Styles of Programming
          2. Representing Natural Numbers
        5. Functional Dependencies
          1. Categories of Products with FDs
          2. Enforcing the Presents Rule with FDs
        6. Type Families
          1. Enforcing the Presents Rule with TFs
          2. Categories of Products with TFs
        7. Data Type Promotion and Singletons
          1. A Further Refinement to the Presents Rule
          2. Enforcing the Duration Rule
        8. Summary
      2. Chapter 14: Interpreting Offers with Attributes
        1. Interpretations and Attribute Grammars
          1. A Simple Interpretation
          2. Introducing Attribute Grammars
        2. Your First Attribute Grammar
          1. Synthesizing the Result
          2. Executing the Attribute Grammar
        3. Integrating UUAGC in Your Package
          1. Installing UUAGC
          2. UUAGC and Cabal
        4. Expressions Interpretation
          1. Using an Attribute Grammar
          2. Precomputing Some Values
          3. A Different (Monadic) View
        5. Offer Interpretations
          1. Checking the Presents Rule
          2. Showing an HTML Description
        6. Origami Programming Over Any Data Type
        7. Summary
    15. Part 5: Engineering the Store
      1. Chapter 15: Documenting, Testing, and Verifying
        1. Documenting Binary Trees with Haddock
        2. Unit Testing with HUnit
          1. Declaring Tests in Cabal
          2. Writing Unit Tests
        3. Randomized Testing with QuickCheck
          1. Testing List Properties
          2. Testing Binary Tree Properties
        4. Formal Verification with Idris
        5. Summary
      2. Chapter 16: Architecting Your Application
        1. Design Patterns and Functional Programming
        2. Medium-Level Guidelines
          1. Use Higher-Order Combinators
          2. Refactor
          3. Use Type Classes Wisely
          4. Enforce Invariants via the Type System
          5. Stay (as) Pure and Polymorphic (as Possible)
        3. Tools
          1. Project and Dependency Management
          2. Code Style
          3. Documentation
          4. Test and Verification
          5. Benchmarking
          6. Profiling
          7. Coverage
          8. Remote Monitoring
        4. Projects
          1. Data Mining Library
          2. Store Network Client
          3. Administration Interface and Tetris
          4. Roll Your Own Monad
        5. Summary
      3. Appendix A: Looking Further
        1. Haskell Resources
          1. Other Functional Languages
      4. Appendix B: Time Traveling with Haskell
    16. Index

    Product information

    • Title: Beginning Haskell: A Project-Based Approach
    • Author(s): Alejandro Serrano Mena
    • Release date: January 2014
    • Publisher(s): Apress
    • ISBN: 9781430262503