Synopses & Reviews
This easy-to-use, fast-moving tutorial introduces you to functional programming with Haskell. You'll learn how to use Haskell in a variety of practical ways, from short scripts to large and demanding applications. Real World Haskell takes you through the basics of functional programming at a brisk pace, and then helps you increase your understanding of Haskell in real-world issues like I/O, performance, dealing with data, concurrency, and more as you move through each chapter.
With this book, you will:
- Understand the differences between procedural and functional programming
- Learn the features of Haskell, and how to use it to develop useful programs
- Interact with filesystems, databases, and network services
- Write solid code with automated tests, code coverage, and error handling
- Harness the power of multicore systems via concurrent and parallel programming
You'll find plenty of hands-on exercises, along with examples of real Haskell programs that you can modify, compile, and run. Whether or not you've used a functional language before, if you want to understand why Haskell is coming into its own as a practical language in so many major organizations, Real World Haskell is the best place to start.
Synopsis
Targeting software developers who want to learn the practical functions of Haskell, this resource introduces the language of the program and pays special attention to issues such as performance, dealing with external data, and Web applications.
About the Author
Bryan O'Sullivan is an Irish hacker and writer who likes distributed systems, open source software, and programming languages. He was a member of the initial design team for the Jini network service architecture (subsequently open sourced as Apache River). He has made significant contributions to, and written a book about, the popular Mercurial revision control system. He lives in San Francisco with his wife and sons. Whenever he can, he runs off to climb rocks.
John Goerzen is an American hacker and author. He has written a number of real-world Haskell libraries and applications, including the HDBC database interface, the ConfigFile configuration file interface, a podcast downloader, and various other libraries relating to networks, parsing, logging, and POSIX code. John has been a developer for the Debian GNU/Linux operating system project for over 10 years and maintains numerous Haskell libraries and code for Debian. He also served as President of Software in the Public Interest, Inc., the legal parent organization of Debian. John lives in rural Kansas with his wife and son, where he enjoys photography and geocaching.
Don Stewart is an Australian hacker based in Portland, Oregon. Don has been involved in a diverse range of Haskell projects, including practical libraries, such as Data.ByteString and Data.Binary, as well as applying the Haskell philosophy to real-world applications including compilers, linkers, text editors, network servers, and systems software. His recent work has focused on optimizing Haskell for high-performance scenarios, using techniques from term rewriting.
Table of Contents
Dedication; Preface; Have We Got a Deal for You!; What to Expect from This Book; What to Expect from Haskell; A Brief Sketch of Haskell's History; Helpful Resources; Conventions Used in This Book; Using Code Examples; Safari® Books Online; How to Contact Us; Acknowledgments; Chapter 1: Getting Started; 1.1 Your Haskell Environment; 1.2 Getting Started with ghci, the Interpreter; 1.3 Basic Interaction: Using ghci as a Calculator; 1.4 Command-Line Editing in ghci; 1.5 Lists; 1.6 Strings and Characters; 1.7 First Steps with Types; 1.8 A Simple Program; Chapter 2: Types and Functions; 2.1 Why Care About Types?; 2.2 Haskell's Type System; 2.3 What to Expect from the Type System; 2.4 Some Common Basic Types; 2.5 Function Application; 2.6 Useful Composite Data Types: Lists and Tuples; 2.7 Functions over Lists and Tuples; 2.8 Function Types and Purity; 2.9 Haskell Source Files, and Writing Simple Functions; 2.10 Understanding Evaluation by Example; 2.11 Polymorphism in Haskell; 2.12 The Type of a Function of More Than One Argument; 2.13 Why the Fuss over Purity?; 2.14 Conclusion; Chapter 3: Defining Types, Streamlining Functions; 3.1 Defining a New Data Type; 3.2 Type Synonyms; 3.3 Algebraic Data Types; 3.4 Pattern Matching; 3.5 Record Syntax; 3.6 Parameterized Types; 3.7 Recursive Types; 3.8 Reporting Errors; 3.9 Introducing Local Variables; 3.10 The Offside Rule and Whitespace in an Expression; 3.11 The case Expression; 3.12 Common Beginner Mistakes with Patterns; 3.13 Conditional Evaluation with Guards; Chapter 4: Functional Programming; 4.1 Thinking in Haskell; 4.2 A Simple Command-Line Framework; 4.3 Warming Up: Portably Splitting Lines of Text; 4.4 Infix Functions; 4.5 Working with Lists; 4.6 How to Think About Loops; 4.7 Anonymous (lambda) Functions; 4.8 Partial Function Application and Currying; 4.9 As-patterns; 4.10 Code Reuse Through Composition; 4.11 Tips for Writing Readable Code; 4.12 Space Leaks and Strict Evaluation; Chapter 5: Writing a Library: Working with JSON Data; 5.1 A Whirlwind Tour of JSON; 5.2 Representing JSON Data in Haskell; 5.3 The Anatomy of a Haskell Module; 5.4 Compiling Haskell Source; 5.5 Generating a Haskell Program and Importing Modules; 5.6 Printing JSON Data; 5.7 Type Inference Is a Double-Edged Sword; 5.8 A More General Look at Rendering; 5.9 Developing Haskell Code Without Going Nuts; 5.10 Pretty Printing a String; 5.11 Arrays and Objects, and the Module Header; 5.12 Writing a Module Header; 5.13 Fleshing Out the Pretty-Printing Library; 5.14 Creating a Package; 5.15 Practical Pointers and Further Reading; Chapter 6: Using Typeclasses; 6.1 The Need for Typeclasses; 6.2 What Are Typeclasses?; 6.3 Declaring Typeclass Instances; 6.4 Important Built-in Typeclasses; 6.5 Automatic Derivation; 6.6 Typeclasses at Work: Making JSON Easier to Use; 6.7 Living in an Open World; 6.8 How to Give a Type a New Identity; 6.9 JSON Typeclasses Without Overlapping Instances; 6.10 The Dreaded Monomorphism Restriction; 6.11 Conclusion; Chapter 7: I/O; 7.1 Classic I/O in Haskell; 7.2 Working with Files and Handles; 7.3 Extended Example: Functional I/O and Temporary Files; 7.4 Lazy I/O; 7.5 The IO Monad; 7.6 Is Haskell Really Imperative?; 7.7 Side Effects with Lazy I/O; 7.8 Buffering; 7.9 Reading Command-Line Arguments; 7.10 Environment Variables; Chapter 8: Efficient File Processing, Regular Expressions, and Filename Matching; 8.1 Efficient File Processing; 8.2 Filename Matching; 8.3 Regular Expressions in Haskell; 8.4 More About Regular Expressions; 8.5 Translating a glob Pattern into a Regular Expression; 8.6 An important Aside: Writing Lazy Functions; 8.7 Making Use of Our Pattern Matcher; 8.8 Handling Errors Through API Design; 8.9 Putting Our Code to Work; Chapter 9: I/O Case Study: A Library for Searching the Filesystem; 9.1 The find Command; 9.2 Starting Simple: Recursively Listing a Directory; 9.3 A Naive Finding Function; 9.4 Predicates: From Poverty to Riches, While Remaining Pure; 9.5 Sizing a File Safely; 9.6 A Domain-Specific Language for Predicates; 9.7 Controlling Traversal; 9.8 Density, Readability, and the Learning Process; 9.9 Another Way of Looking at Traversal; 9.10 Useful Coding Guidelines; Chapter 10: Code Case Study: Parsing a Binary Data Format; 10.1 Grayscale Files; 10.2 Parsing a Raw PGM File; 10.3 Getting Rid of Boilerplate Code; 10.4 Implicit State; 10.5 Introducing Functors; 10.6 Writing a Functor Instance for Parse; 10.7 Using Functors for Parsing; 10.8 Rewriting Our PGM Parser; 10.9 Future Directions; Chapter 11: Testing and Quality Assurance; 11.1 QuickCheck: Type-Based Testing; 11.2 Testing Case Study: Specifying a Pretty Printer; 11.3 Measuring Test Coverage with HPC; Chapter 12: Barcode Recognition; 12.1 A Little Bit About Barcodes; 12.2 Introducing Arrays; 12.3 Encoding an EAN-13 Barcode; 12.4 Constraints on Our Decoder; 12.5 Divide and Conquer; 12.6 Turning a Color Image into Something Tractable; 12.7 What Have We Done to Our Image?; 12.8 Finding Matching Digits; 12.9 Life Without Arrays or Hash Tables; 12.10 Turning Digit Soup into an Answer; 12.11 Working with Row Data; 12.12 Pulling It All Together; 12.13 A Few Comments on Development Style; Chapter 13: Data Structures; 13.1 Association Lists; 13.2 Maps; 13.3 Functions Are Data, Too; 13.4 Extended Example: /etc/passwd; 13.5 Extended Example: Numeric Types; 13.6 Taking Advantage of Functions as Data; 13.7 General-Purpose Sequences; Chapter 14: Monads; 14.1 ; 14.2 Revisiting Earlier Code Examples; 14.3 Looking for Shared Patterns; 14.4 The Monad Typeclass; 14.5 And Now, a Jargon Moment; 14.6 Using a New Monad: Show Your Work!; 14.7 Mixing Pure and Monadic Code; 14.8 Putting a Few Misconceptions to Rest; 14.9 Building the Logger Monad; 14.10 The Maybe Monad; 14.11 The List Monad; 14.12 Desugaring of do Blocks; 14.13 The State Monad; 14.14 Monads and Functors; 14.15 The Monad Laws and Good Coding Style; Chapter 15: Programming with Monads; 15.1 Golfing Practice: Association Lists; 15.2 Generalized Lifting; 15.3 Looking for Alternatives; 15.4 Adventures in Hiding the Plumbing; 15.5 Separating Interface from Implementation; 15.6 The Reader Monad; 15.7 A Return to Automated Deriving; 15.8 Hiding the IO Monad; Chapter 16: Using Parsec; 16.1 First Steps with Parsec: Simple CSV Parsing; 16.2 The sepBy and endBy Combinators; 16.3 Choices and Errors; 16.4 Extended Example: Full CSV Parser; 16.5 Parsec and MonadPlus; 16.6 Parsing a URL-Encoded Query String; 16.7 Supplanting Regular Expressions for Casual Parsing; 16.8 Parsing Without Variables; 16.9 Applicative Functors for Parsing; 16.10 Applicative Parsing by Example; 16.11 Parsing JSON Data; 16.12 Parsing a HTTP Request; Chapter 17: Interfacing with C: The FFI; 17.1 Foreign Language Bindings: The Basics; 17.2 Regular Expressions for Haskell: A Binding for PCRE; 17.3 Passing String Data Between Haskell and C; 17.4 Matching on Strings; Chapter 18: Monad Transformers; 18.1 Motivation: Boilerplate Avoidance; 18.2 A Simple Monad Transformer Example; 18.3 Common Patterns in Monads and Monad Transformers; 18.4 Stacking Multiple Monad Transformers; 18.5 Moving Down the Stack; 18.6 Understanding Monad Transformers by Building One; 18.7 Transformer Stacking Order Is Important; 18.8 Putting Monads and Monad Transformers into Perspective; Chapter 19: Error Handling; 19.1 Error Handling with Data Types; 19.2 Exceptions; 19.3 Error Handling in Monads; Chapter 20: Systems Programming in Haskell; 20.1 Running External Programs; 20.2 Directory and File Information; 20.3 Program Termination; 20.4 Dates and Times; 20.5 Extended Example: Piping; Chapter 21: Using Databases; 21.1 Overview of HDBC; 21.2 Installing HDBC and Drivers; 21.3 Connecting to Databases; 21.4 Transactions; 21.5 Simple Queries; 21.6 SqlValue; 21.7 Query Parameters; 21.8 Prepared Statements; 21.9 Reading Results; 21.10 Database Metadata; 21.11 Error Handling; Chapter 22: Extended Example: Web Client Programming; 22.1 Basic Types; 22.2 The Database; 22.3 The Parser; 22.4 Downloading; 22.5 Main Program; Chapter 23: GUI Programming with gtk2hs; 23.1 Installing gtk2hs; 23.2 Overview of the GTK+ Stack; 23.3 User Interface Design with Glade; 23.4 Event-Driven Programming; 23.5 Initializing the GUI; 23.6 The Add Podcast Window; 23.7 Long-Running Tasks; 23.8 Using Cabal; Chapter 24: Concurrent and Multicore Programming; 24.1 Defining Concurrency and Parallelism; 24.2 Concurrent Programming with Threads; 24.3 Simple Communication Between Threads; 24.4 The Main Thread and Waiting for Other Threads; 24.5 Communicating over Channels; 24.6 Useful Things to Know About; 24.7 Shared-State Concurrency Is Still Hard; 24.8 Using Multiple Cores with GHC; 24.9 Parallel Programming in Haskell; 24.10 Parallel Strategies and MapReduce; Chapter 25: Profiling and Optimization; 25.1 Profiling Haskell Programs; 25.2 Controlling Evaluation; 25.3 Understanding Core; 25.4 Advanced Techniques: Fusion; Chapter 26: Advanced Library Design: Building a Bloom Filter; 26.1 Introducing the Bloom Filter; 26.2 Use Cases and Package Layout; 26.3 Basic Design; 26.4 The ST Monad; 26.5 Designing an API for Qualified Import; 26.6 Creating a Mutable Bloom Filter; 26.7 The Immutable API; 26.8 Creating a Friendly Interface; 26.9 Creating a Cabal Package; 26.10 Testing with QuickCheck; 26.11 Performance Analysis and Tuning; Chapter 27: Sockets and Syslog; 27.1 Basic Networking; 27.2 Communicating with UDP; 27.3 Communicating with TCP; Chapter 28: Software Transactional Memory; 28.1 The Basics; 28.2 Some Simple Examples; 28.3 STM and Safety; 28.4 Retrying a Transaction; 28.5 Choosing Between Alternatives; 28.6 I/O and STM; 28.7 Communication Between Threads; 28.8 A Concurrent Web Link Checker; 28.9 Practical Aspects of STM; Installing GHC and Haskell Libraries; Installing GHC; Installing Haskell Software; Characters, Strings, and Escaping Rules; Writing Character and String Literals; International Language Support; Escaping Text; Colophon;