If you’re new to Erlang, its functional style can seem difficult, but with help from this hands-on introduction, you’ll scale the learning curve and discover how enjoyable, powerful, and fun this language can be. Author Simon St. Laurent shows you how to write simple Erlang programs by teaching you one basic skill at a time. You’ll learn about pattern matching, recursion, message passing, process-oriented programming, and establishing pathways for data rather than telling it where to go. By the end of your journey, you’ll understand why Erlang is ideal for concurrency and resilience. Get cozy with Erlang’s shell, its command line interface Become familiar with Erlang’s basic structures by working with numbers Discover atoms, pattern matching, and guards: the foundations of your program structure Delve into the heart of Erlang processing with recursion, strings, lists, and higher-order functions Create processes, send messages among them, and apply pattern matching to incoming messages Store and manipulate structured data with Erlang Term Storage and the Mnesia database Learn about Open Telecom Platform, Erlang’s open source libraries and tools Copyright 4 Table of Contents 5 Preface 9 Who This Book Is For 10 Who This Book Is Not For 10 What This Book Will Do For You 10 How This Book Works 11 Why I Wrote This Book 11 Other Resources 11 Are You Sure You Want Erlang? 12 Erlang Will Change You 13 Conventions Used in This Book 13 A Note on Erlang Syntax 14 Using Code Examples 14 Help This Book Grow 15 Please Use It For Good 15 Safari庐 Books Online 15 How to Contact Us 16 Acknowledgments 16 Chapter聽1.聽Getting Comfortable 17 Installation 17 Firing It Up 18 First Steps: The Shell 18 Moving through Text 19 Moving through History 19 Moving through Files 20 Doing Something 20 Calling Functions 21 Numbers in Erlang 22 Working with Variables in the Shell 24 Seeing Your Bound Variables 26 Clearing Bound Variables in the Shell 26 Chapter聽2.聽Functions and Modules 29 Fun with fun 29 Defining Modules 31 From Module to Fun 33 Functions and Variable Scope 34 Module Directives 34 Documenting Code 36 Documenting Modules 37 Documenting Functions 38 Documenting Your Application 40 Chapter聽3.聽Atoms, Tuples, and Pattern Matching 43 Atoms 43 Pattern Matching with Atoms 44 Atomic Booleans 45 Guards 46 Underscoring That You Don鈥檛 Care 48 Adding Structure: Tuples 50 Pattern Matching with Tuples 51 Processing Tuples 52 Chapter聽4.聽Logic and Recursion 55 Logic Inside of Functions 55 Evaluating Cases 55 If This, Then That 58 Variable Assignment in case and if Constructs 60 The Gentlest Side Effect: io:format 61 Simple Recursion 62 Counting Down 62 Counting Up 63 Recursing with Return Values 65 Chapter聽5.聽Communicating with Humans 69 Strings 69 Asking Users for Information 71 Gathering Terms 72 Gathering Characters 75 Reading Lines of Text 76 Chapter聽6.聽Lists 81 List Basics 81 Splitting Lists into Heads and Tails 83 Processing List Content 84 Creating Lists with Heads and Tails 86 Mixing Lists and Tuples 88 Building a List of Lists 89 Chapter聽7.聽Higher-Order Functions and List Comprehensions 93 Simple Higher-Order Functions 93 Creating New Lists with Higher-Order Functions 95 Reporting on a List 95 Running List Values Through a Function 96 Filtering List Values 97 Beyond List Comprehensions 97 Testing Lists 98 Splitting Lists 98 Folding Lists 99 Chapter聽8.聽Playing with Processes 103 The Shell Is a Process 103 Spawning Processes from Modules 105 Lightweight Processes 108 Registering a Process 108 When Processes Break 110 Processes Talking Amongst Themselves 111 Watching Your Processes 113 Breaking Things and Linking Processes 115 Chapter聽9.聽Exceptions, Errors, and Debugging 123 Flavors of Errors 123 Catching Runtime Errors as They Happen 124 Raising Exceptions with throw 126 Logging Progress and Failure 127 Debugging through a GUI 128 Tracing Messages 135 Watching Function Calls 138 Chapter聽10.聽Storing Structured Data 141 From Tuples to Records 141 Setting Up Records 142 Creating and Reading Records 143 Using Records in Functions and Modules 144 Storing Records in Erlang Term Storage 147 Creating and Populating a Table 148 Simple Queries 153 A Key Feature: Overwriting Values 154 ETS Tables and Processes 154 Next Steps 156 Storing Records in Mnesia 157 Starting up Mnesia 157 Creating Tables 158 Reading Data 161 Query List Comprehensions 162 Chapter聽11.聽Getting Started with OTP 167 Creating Services with gen_server 168 A Simple Supervisor 173 Packaging an Application 178 Chapter聽12.聽Next Steps Through Erlang 181 Moving Beyond the Shell 181 Distributed Computing 182 Processing Binary Data 182 Input and Output 182 Testing, Analyzing, and Refactoring 182 Networking and the Web 183 Data Storage 183 Extending Erlang 184 Languages Built on Erlang 184 Community 184 Sharing the Gospel of Erlang 185 Appendix聽A.聽An Erlang Parts Catalog 187 Shell Commands 187 Reserved Words 188 Operators 189 Guard Components 191 Common Functions 191 Strings and Formatting 192 Data Types for Documentation and Analysis 194 Appendix聽B.聽OTP Templates 195 About the Author 201 If you’re new to Erlang, its functional style can seem difficult, but with help from this hands-on introduction, you’ll scale the learning curve and discover how enjoyable, powerful, and fun this language can be.Author Simon St. Laurent shows you how to write simple Erlang programs by teaching you one basic skill at a time. You’ll learn about pattern matching, recursion, message passing, process-oriented programming, and establishing pathways for data rather than telling it where to go. By the end of your journey, you’ll understand why Erlang is ideal for concurrency and resilience.Get cozy with Erlang’s shell, its command line interfaceBecome familiar with Erlang’s basic structures by working with numbersDiscover atoms, pattern matching, and guards: the foundations of your program structureDelve into the heart of Erlang processing with recursion, strings, lists, and higher-order functionsCreate processes, send messages among them, and apply pattern matching to incoming messagesStore and manipulate structured data with Erlang Term Storage and the Mnesia databaseLearn about Open Telecom Platform, Erlang’s open source libraries and tools 'Introducing Erlang' gives readers a gentle introduction to this powerful and reliable functional language. Examples demonstrate how to make Erlang work on its own terms, rather than attempting to translate from other languages