Haskell Design Patterns
eBook - ePub

Haskell Design Patterns

Ryan Lemmer

Share book
  1. 166 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Haskell Design Patterns

Ryan Lemmer

Book details
Book preview
Table of contents
Citations

About This Book

Take your Haskell and functional programming skills to the next level by exploring new idioms and design patterns

About This Book

  • Explore Haskell on a higher level through idioms and patterns
  • Get an in-depth look into the three strongholds of Haskell: higher-order functions, the Type system, and Lazy evaluation
  • Expand your understanding of Haskell and functional programming, one line of executable code at a time

Who This Book Is For

If you're a Haskell programmer with a firm grasp of the basics and ready to move more deeply into modern idiomatic Haskell programming, then this book is for you.

What You Will Learn

  • Understand the relationship between the "Gang of Four" OOP Design Patterns and Haskell
  • Try out three ways of Streaming I/O: imperative, Lazy, and Iteratee based
  • Explore the pervasive pattern of Composition: from function composition through to high-level composition with Lenses
  • Synthesize Functor, Applicative, Arrow and Monad in a single conceptual framework
  • Follow the grand arc of Fold and Map on lists all the way to their culmination in Lenses and Generic Programming
  • Get a taste of Type-level programming in Haskell and how this relates to dependently-typed programming
  • Retrace the evolution, one key language extension at a time, of the Haskell Type and Kind systems
  • Place the elements of modern Haskell in a historical framework

In Detail

Design patterns and idioms can widen our perspective by showing us where to look, what to look at, and ultimately how to see what we are looking at. At their best, patterns are a shorthand method of communicating better ways to code (writing less, more maintainable, and more efficient code).

This book starts with Haskell 98 and through the lens of patterns and idioms investigates the key advances and programming styles that together make "modern Haskell". Your journey begins with the three pillars of Haskell. Then you'll experience the problem with Lazy I/O, together with a solution. You'll also trace the hierarchy formed by Functor, Applicative, Arrow, and Monad. Next you'll explore how Fold and Map are generalized by Foldable and Traversable, which in turn is unified in a broader context by functional Lenses. You'll delve more deeply into the Type system, which will prepare you for an overview of Generic programming. In conclusion you go to the edge of Haskell by investigating the Kind system and how this relates to Dependently-typed programming.

Style and approach

Using short pieces of executable code, this guide gradually explores the broad pattern landscape of modern Haskell. Ideas are presented in their historical context and arrived at through intuitive derivations, always with a focus on the problems they solve.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Haskell Design Patterns an online PDF/ePUB?
Yes, you can access Haskell Design Patterns by Ryan Lemmer in PDF and/or ePUB format, as well as other popular books in Computer Science & Open Source Programming. We have over one million books available in our catalogue for you to explore.

Information

Year
2015
ISBN
9781783988723
Edition
1

Haskell Design Patterns


Table of Contents

Haskell Design Patterns
Credits
About the Author
About the Reviewer
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Functional Patterns – the Building Blocks
Higher-order functions
Functions as first-class citizens
Composing functions
Currying functions
Currying and composability
Decoupling with currying
Recursion
Non-tail recursion
Tail recursion
Folding abstracts recursion
Types, pattern matching, and polymorphism
Algebraic types and pattern matching
Recursive types
Polymorphism
Parametric polymorphism
Ad-hoc polymorphism
Alternation-based ad-hoc polymorphism
Class-based ad-hoc polymorphism
Alternation-based versus class-based
Polymorphic dispatch and the visitor pattern
Unifying parametric and ad-hoc polymorphism
Functions, types, and patterns
The strategy pattern
The template pattern
The iterator pattern
Decoupling behavior and modularizing code
Lazy evaluation
Streams
Modeling change with streams
Lazy evil
Monads
Composing monads and structuring programs
Summary
2. Patterns for I/O
I/O as a first class citizen
I/O as a functor, applicative, and monad
Imperative I/O
Lazy I/O
The problems with lazy I/O
Resource management with bracket
Iteratee I/O
Iteratee
Enumerator
Generalized iteratees, enumerators, and enumeratees
The iteratee I/O libraries
Comparing the three styles of I/O
Summary
3. Patterns of Composition
Functor
Applicative functor
Monad
Monad as functor
Monad as applicative
Sequencing actions with monad and applicative
Monads and the bind chain
Composing with monads
Monad transformers
IO in monad stacks
Sequence of stack composition
Arrows
Implementing an arrow
Arrow operators
Kleisli arrows and monad arrows
Why arrows?
Summary
4. Patterns of Folding and Traversing
Folding over lists
Folding with monadic functions
Folding with monoids
Foldable
Mapping over lists
Traversable
A Traversable Tree
The traversal and the Iterator pattern
Modernizing Haskell 98
Lenses
Deriving Lens
Writing a Lens
Composable getters and setters
Lens Traversal
Lens.Fold
The Lens library
Summary
5. Patterns of Type Abstraction
Abstracting function types: RankNTypes
Abstracting datatypes
Universal quantification
Existential quantification and abstract datatypes
Phantom types
Generalized algebraic datatypes
Typecase pattern
Dynamic types
Heterogeneous lists
Using existentials
Using GADTs
Abstracting type-classes
Multiparameter type-classes
Functional dependencies
Summary
6. Patterns of Generic Programming
Patterns of generic programming
Patterns 1 and 2 – functions
Pattern 3 – polymorphic types and functions
Pattern 4 – type-class polymorphism
Pattern 5 – meta-programming
The Derivable type-classes
Generalized newtype deriving
Pattern 6 – type laws
Pattern 7 – datatype generic programming
The sum of products style
The sum of products type representation
Translating between the type and representation
Writing a datatype-generic function
Adding a new datatype
GHC.Generics – a generic deriving mechanism
Origami programming
Tying the recursive knot
The generic map
The generic fold
main = print $ gfold addL aListF
Generic unfold and fold
Origami design patterns
Scrap your boilerplate
The type-safe cast with typeable
Type-safe function application
The shallow traversal and the data type-class
Typeable and data
Scrap your boilerplate
Summary
7. Patterns of Kind Abstraction
Higher-order kinds
Higher-kinded polymorphism
Associated type synonyms
Using functional dependencies
Associated type synonyms
Associated types versus functional dependencies
Type (synonym) families
Data families
Kind polymorphism
The PolyKinds language extension
Type promotion
Promoting types to kinds
Type-level programming
Promoting term-level programs to type-level
Closed type families
The history of type-level programming in Haskell
Type-level and generic programming
Dependently-typed programming
Haskell and dependently-typed programming
Summary
Epilogue
Index

Haskell Design Patterns

Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing and its dealers and distributors will be h...

Table of contents