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.
Chapter 1 Getting Started
Your Haskell Environment
Getting Started with ghci, the Interpreter
Basic Interaction: Using ghci as a Calculator
Command-Line Editing in ghci
Strings and Characters
First Steps with Types
A Simple Program
Chapter 2 Types and Functions
Why Care About Types?
Haskell’s Type System
What to Expect from the Type System
Some Common Basic Types
Useful Composite Data Types: Lists and Tuples
Functions over Lists and Tuples
Function Types and Purity
Haskell Source Files, and Writing Simple Functions
Understanding Evaluation by Example
Polymorphism in Haskell
The Type of a Function of More Than One Argument
Why the Fuss over Purity?
Chapter 3 Defining Types, Streamlining Functions
Defining a New Data Type
Algebraic Data Types
Introducing Local Variables
The Offside Rule and Whitespace in an Expression
The case Expression
Common Beginner Mistakes with Patterns
Conditional Evaluation with Guards
Chapter 4 Functional Programming
Thinking in Haskell
A Simple Command-Line Framework
Warming Up: Portably Splitting Lines of Text
Working with Lists
How to Think About Loops
Anonymous (lambda) Functions
Partial Function Application and Currying
Code Reuse Through Composition
Tips for Writing Readable Code
Space Leaks and Strict Evaluation
Chapter 5 Writing a Library: Working with JSON Data
A Whirlwind Tour of JSON
Representing JSON Data in Haskell
The Anatomy of a Haskell Module
Compiling Haskell Source
Generating a Haskell Program and Importing Modules
Printing JSON Data
Type Inference Is a Double-Edged Sword
A More General Look at Rendering
Developing Haskell Code Without Going Nuts
Pretty Printing a String
Arrays and Objects, and the Module Header
Writing a Module Header
Fleshing Out the Pretty-Printing Library
Creating a Package
Practical Pointers and Further Reading
Chapter 6 Using Typeclasses
The Need for Typeclasses
What Are Typeclasses?
Declaring Typeclass Instances
Important Built-in Typeclasses
Typeclasses at Work: Making JSON Easier to Use
Living in an Open World
How to Give a Type a New Identity
JSON Typeclasses Without Overlapping Instances
The Dreaded Monomorphism Restriction
Chapter 7 I/O
Classic I/O in Haskell
Working with Files and Handles
Extended Example: Functional I/O and Temporary Files
The IO Monad
Is Haskell Really Imperative?
Side Effects with Lazy I/O
Reading Command-Line Arguments
Chapter 8 Efficient File Processing, Regular Expressions, and Filename Matching
Efficient File Processing
Regular Expressions in Haskell
More About Regular Expressions
Translating a glob Pattern into a Regular Expression
An important Aside: Writing Lazy Functions
Making Use of Our Pattern Matcher
Handling Errors Through API Design
Putting Our Code to Work
Chapter 9 I/O Case Study: A Library for Searching the Filesystem
The find Command
Starting Simple: Recursively Listing a Directory
A Naive Finding Function
Predicates: From Poverty to Riches, While Remaining Pure
Sizing a File Safely
A Domain-Specific Language for Predicates
Density, Readability, and the Learning Process
Another Way of Looking at Traversal
Useful Coding Guidelines
Chapter 10 Code Case Study: Parsing a Binary Data Format
Parsing a Raw PGM File
Getting Rid of Boilerplate Code
Writing a Functor Instance for Parse
Using Functors for Parsing
Rewriting Our PGM Parser
Chapter 11 Testing and Quality Assurance
QuickCheck: Type-Based Testing
Testing Case Study: Specifying a Pretty Printer
Measuring Test Coverage with HPC
Chapter 12 Barcode Recognition
A Little Bit About Barcodes
Encoding an EAN-13 Barcode
Constraints on Our Decoder
Divide and Conquer
Turning a Color Image into Something Tractable
What Have We Done to Our Image?
Finding Matching Digits
Life Without Arrays or Hash Tables
Turning Digit Soup into an Answer
Working with Row Data
Pulling It All Together
A Few Comments on Development Style
Chapter 13 Data Structures
Functions Are Data, Too
Extended Example: /etc/passwd
Extended Example: Numeric Types
Taking Advantage of Functions as Data
Chapter 14 Monads
Revisiting Earlier Code Examples
Looking for Shared Patterns
The Monad Typeclass
And Now, a Jargon Moment
Using a New Monad: Show Your Work!
Mixing Pure and Monadic Code
Putting a Few Misconceptions to Rest
Building the Logger Monad
The Maybe Monad
The List Monad
Desugaring of do Blocks
The State Monad
Monads and Functors
The Monad Laws and Good Coding Style
Chapter 15 Programming with Monads
Golfing Practice: Association Lists
Looking for Alternatives
Adventures in Hiding the Plumbing
Separating Interface from Implementation
The Reader Monad
A Return to Automated Deriving
Hiding the IO Monad
Chapter 16 Using Parsec
First Steps with Parsec: Simple CSV Parsing
The sepBy and endBy Combinators
Choices and Errors
Extended Example: Full CSV Parser
Parsec and MonadPlus
Parsing a URL-Encoded Query String
Supplanting Regular Expressions for Casual Parsing
Parsing Without Variables
Applicative Functors for Parsing
Applicative Parsing by Example
Parsing JSON Data
Parsing a HTTP Request
Chapter 17 Interfacing with C: The FFI
Foreign Language Bindings: The Basics
Regular Expressions for Haskell: A Binding for PCRE
Passing String Data Between Haskell and C
Matching on Strings
Chapter 18 Monad Transformers
Motivation: Boilerplate Avoidance
A Simple Monad Transformer Example
Common Patterns in Monads and Monad Transformers
Stacking Multiple Monad Transformers
Moving Down the Stack
Understanding Monad Transformers by Building One
Transformer Stacking Order Is Important
Putting Monads and Monad Transformers into Perspective
Chapter 19 Error Handling
Error Handling with Data Types
Error Handling in Monads
Chapter 20 Systems Programming in Haskell
Running External Programs
Directory and File Information
Dates and Times
Extended Example: Piping
Chapter 21 Using Databases
Overview of HDBC
Installing HDBC and Drivers
Connecting to Databases
Chapter 22 Extended Example: Web Client Programming
Chapter 23 GUI Programming with gtk2hs
Overview of the GTK+ Stack
User Interface Design with Glade
Initializing the GUI
The Add Podcast Window
Chapter 24 Concurrent and Multicore Programming
Defining Concurrency and Parallelism
Concurrent Programming with Threads
Simple Communication Between Threads
The Main Thread and Waiting for Other Threads
Communicating over Channels
Useful Things to Know About
Shared-State Concurrency Is Still Hard
Using Multiple Cores with GHC
Parallel Programming in Haskell
Parallel Strategies and MapReduce
Chapter 25 Profiling and Optimization
Profiling Haskell Programs
Advanced Techniques: Fusion
Chapter 26 Advanced Library Design: Building a Bloom Filter
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.
The animal on the cover of Real World Haskell is a rhinoceros beetle, a species of scarab beetle. Relative to their size, rhinoceros beetles are among the strongest animals on the planet. They can lift up to 850 times their own weight. The average rhino beetle found in the U.S. is about an inch long, but they can grow as long as seven inches.
Rhino beetles have horns on their heads, resembling that of the rhinoceros, hence the name. The size of their horns is related to how much nutrition they had in larva. In some species, the horns are longer than the bodies, and they can grow as many as four or five horns. They use the horns for digging, as well as for fighting for territory and mates.
Rhino beetles thrive on sap and rotting fruit, specifically bananas, apples, and oranges. Their larvae, which takes between 3-5 years to mature, eat decaying wood, compost, and dead leaves-a kind of recycling for the environment.
This book was published in November 2008, when GHC 6.10.1 was released. Through November 2015 there have been TWO DOZEN GHC releases. To put that in perspective, Python 2.6 was also released in October 2008. Books and from those long-ago times are of limited value in understanding Python 3.5.1, released in December 2015 or GHC 7.10.3 the same month.
On the other hand, __examples__ are littered with version compatibility problems. When you are trying to reproduce the parsec example in Chapter 16 is no time to spend your effort in bug hunting. (Have a go at JSONParsec.hs to see what I mean.) While an expert may find some chuckles value, for the beginner it's a time sink, the examples have anti-value.
O'Reilly should withdraw this title from publication and solicit a new edition.
Bottom Line No, I would not recommend this to a friend
Real World Haskell is a no-brainer for the programmer who wants to start programming in the functional paradigm but also a good book for the actual Haskell programmer. It can be used as a reference book because it covers almost all of the Haskell features. You may want to look for the Monad's definition, you may want to remember how Haskell deal with STM and you'll find it all there.
The book is a book for programmers used to the imperative paradigm because it follows a path that helps the functional programming newbie to understand how and why things are done, however I wouldn't recommend the same book for programming newbies. In that case the book you're looking for is Learn You A Haskell for Greater Good!
What I found really interesting in this book is that it starts easy, small, but it shows how haskell can be used in the real world ( just like the name suggests). It teaches you how to create lib's and modules using JSON, how to work with files, error handling, database connection. GUI programming, etc and with that you are able to create a real system just like you would in the imperative paradigm.
I consider Haskell the best language for someone that wants to learn a functional programming language because
it's very easy to understand, very powerful and beautiful! I've been using this book as reference guide for two months and I recommend it to every Haskell programmer.
Bottom Line Yes, I would recommend this to a friend
I consider myself pretty decent in erlang after over a year now playing with it and I've been programming other languages for years. I had glanced at haskell every now and then, initially reading the first few chapters of this book online. I ordered the book later on but now I'm finding myself constantly going back to the online comments for elaboration, making me read the book online anyway. I don't think a lot of the examples are that great. The "learn you a haskell" tutorial by BONUS seems to have much better examples and was much more accessible for me. This is still a worthwhile read but mostly in combination with the online comments others have made on the site. I only give it 4 because I consider BONUS's online guide much better.
Bottom Line Yes, I would recommend this to a friend
Good Book for those stepping into the Haskell world
By Jeff Bergman
Comments about oreilly Real World Haskell:
Real World Haskell is very ambitious in its scope. It tries to gradually introduce the Haskell way of doing things such that even someone coming from an imperative programming background can follow.
As a consequence some concepts are not formally explained until later in the book, like Monads. Instead the book shows you how to use Haskell's I/O facilities, without an understanding of Monads, first.
For some this approach is probably very practical but I found myself at times wanting the material to be presented in a different order.
However, I am still giving this book 5 stars because of the sheer breadth and quality of the content and examples. And the later chapters really do tie all the concepts together with some non-trivial examples.
The first four chapters and chapter six lay the foundation for the rest of the book. I found that a good understanding of this material was crucial for later chapters, where they combine different features of the language in more complicated ways.
After that I was particularly fond of chapters 10, 13,14, 15, 16, 18, and 26, as these chapters explained some of the more advanced concepts I was interested in like Monads, Parsing, and Functional Data Structures.
Overall, I learned a ton of new things from reading this book
even thought the material is quite challenging in places, and found myself wondering why more people don't use Haskell.