Real World Haskell Real World Haskell Bryan O’Sullivan, John Goerzen, and Don Stewart Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo Real World Haskell by Bryan O’Sullivan, John Goerzen, and Don Stewart Copyright © 2009 Bryan O’Sullivan, John Goerzen, and Donald Stewart. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safari.oreilly.com). For more information, contact our corporate/ institutional sales department: (800) 998-9938 or [email protected]. Editor: Mike Loukides Indexer: Joe Wizda Production Editor: Loranah Dimant Cover Designer: Karen Montgomery Copyeditor: Mary Brady Interior Designer: David Futato Proofreader: Loranah Dimant Illustrator: Robert Romano Printing History: November 2008: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Real World Haskell, the image of a rhinoceros beetle, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. TM This book uses RepKover™, a durable and flexible lay-flat binding. ISBN: 978-0-596-51498-3 [M] 1226696198 To Cian, Ruairi, and Shannon, for the love and joy they bring. —Bryan For my wife, Terah, with thanks for all her love, encouragement, and support. —John To Suzie, for her love and support. —Don Table of Contents Preface ................................................................... xxiii 1. Getting Started ......................................................... 1 Your Haskell Environment 1 Getting Started with ghci, the Interpreter 2 Basic Interaction: Using ghci as a Calculator 3 Simple Arithmetic 3 An Arithmetic Quirk: Writing Negative Numbers 4 Boolean Logic, Operators, and Value Comparisons 5 Operator Precedence and Associativity 7 Undefined Values, and Introducing Variables 8 Dealing with Precedence and Associativity Rules 8 Command-Line Editing in ghci 9 Lists 9 Operators on Lists 11 Strings and Characters 11 First Steps with Types 12 A Simple Program 15 2. Types and Functions .................................................... 17 Why Care About Types? 17 Haskell’s Type System 18 Strong Types 18 Static Types 19 Type Inference 20 What to Expect from the Type System 20 Some Common Basic Types 21 Function Application 22 Useful Composite Data Types: Lists and Tuples 23 Functions over Lists and Tuples 25 Passing an Expression to a Function 26 Function Types and Purity 27 vii Haskell Source Files, and Writing Simple Functions 27 Just What Is a Variable, Anyway? 28 Conditional Evaluation 29 Understanding Evaluation by Example 32 Lazy Evaluation 32 A More Involved Example 33 Recursion 34 Ending the Recursion 35 Returning from the Recursion 35 What Have We Learned? 36 Polymorphism in Haskell 36 Reasoning About Polymorphic Functions 38 Further Reading 38 The Type of a Function of More Than One Argument 38 Why the Fuss over Purity? 39 Conclusion 40 3. Defining Types, Streamlining Functions ................................... 41 Defining a New Data Type 41 Naming Types and Values 43 Type Synonyms 43 Algebraic Data Types 44 Tuples, Algebraic Data Types, and When to Use Each 45 Analogues to Algebraic Data Types in Other Languages 47 Pattern Matching 50 Construction and Deconstruction 51 Further Adventures 52 Variable Naming in Patterns 53 The Wild Card Pattern 53 Exhaustive Patterns and Wild Cards 54 Record Syntax 55 Parameterized Types 57 Recursive Types 58 Reporting Errors 60 A More Controlled Approach 61 Introducing Local Variables 61 Shadowing 62 The where Clause 63 Local Functions, Global Variables 63 The Offside Rule and Whitespace in an Expression 64 A Note About Tabs Versus Spaces 66 The Offside Rule Is Not Mandatory 66 The case Expression 66 viii | Table of Contents