ebook img

Functional Programming for Dummies PDF

2019·8.09 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Functional Programming for Dummies

Functional Programming by John Paul Mueller Functional Programming For Dummies® Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com Copyright © 2019 by John Wiley & Sons, Inc., Hoboken, New Jersey Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/ go/permissions. Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit https://hub.wiley.com/community/support/dummies. Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com. Library of Congress Control Number: 2018965285 ISBN: 978-1-119-52750-3 ISBN 978-1-119-52751-0 (ebk); ISBN ePDF 978-1-119-52749-7 (ebk) Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 Table of Contents INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Foolish Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Icons Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Beyond the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Where to Go from Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 PART 1: GETTING STARTED WITH FUNCTIONAL PROGRAMMING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 CHAPTER 1: Introducing Functional Programming . . . . . . . . . . . . . . . . 9 Defining Functional Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Understanding its goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Using the pure approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Using the impure approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Considering Other Programming Paradigms . . . . . . . . . . . . . . . . . . . . . 13 Imperative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13 Procedural . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Object-oriented . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Declarative . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Using Functional Programming to Perform Tasks . . . . . . . . . . . . . . . . . 15 Discovering Languages That Support Functional Programming . . . . . 16 Considering the pure languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Considering the impure languages . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Finding Functional Programming Online . . . . . . . . . . . . . . . . . . . . . . . . . 17 CHAPTER 2: Getting and Using Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Working with Python in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Creating better code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Debugging functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Defining why notebooks are useful . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Obtaining Your Copy of Anaconda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Obtaining Analytics Anaconda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Installing Anaconda on Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Installing Anaconda on MacOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Installing Anaconda on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Understanding the Anaconda package . . . . . . . . . . . . . . . . . . . . . . . 26 Downloading the Datasets and Example Code . . . . . . . . . . . . . . . . . . . . 27 Using Jupyter Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Defining the code repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Getting and using datasets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Table of Contents iii Creating a Python Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Understanding cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35 Adding documentation cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Other cell content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Running the Python Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Understanding the Use of Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Adding Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Understanding comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Using comments to leave yourself reminders . . . . . . . . . . . . . . . . . 43 Using comments to keep code from executing . . . . . . . . . . . . . . . . 43 Closing Jupyter Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44 Getting Help with the Python Language . . . . . . . . . . . . . . . . . . . . . . . . . 45 CHAPTER 3: Getting and Using Haskell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Working with Haskell in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Obtaining and Installing Haskell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Installing Haskell on a Linux system . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Installing Haskell on a Mac system . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Installing Haskell on a Windows system . . . . . . . . . . . . . . . . . . . . . . 52 Testing the Haskell Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Compiling a Haskell Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Using Haskell Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Getting Help with the Haskell Language . . . . . . . . . . . . . . . . . . . . . . . . . 60 PART 2: STARTING FUNCTIONAL PROGRAMMING TASKS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6 .3 CHAPTER 4: Defining the Functional Difference . . . . . . . . . . . . . . . . . . 65 Comparing Declarations to Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Understanding How Data Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67 Working with immutable data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Considering the role of state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Eliminating side effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Seeing a Function in Haskell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Using non-curried functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Using curried functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Seeing a Function in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73 Creating and using a Python function . . . . . . . . . . . . . . . . . . . . . . . . 73 Passing by reference versus by value . . . . . . . . . . . . . . . . . . . . . . . . . 74 CHAPTER 5: Understanding the Role of Lambda Calculus . . . . . . . 77 Considering the Origins of Lambda Calculus . . . . . . . . . . . . . . . . . . . . . 78 Understanding the Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Working with variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Using application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Using abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 iv Functional Programming For Dummies Performing Reduction Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Considering α-conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Considering β-reduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Considering η-conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Creating Lambda Functions in Haskell . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Creating Lambda Functions in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 CHAPTER 6: Working with Lists and Strings . . . . . . . . . . . . . . . . . . . . . . . . 91 Defining List Uses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Creating Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Using Haskell to create Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Using Python to create lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Evaluating Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Using Haskell to evaluate Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Using Python to evaluate lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Performing Common List Manipulations . . . . . . . . . . . . . . . . . . . . . . . .100 Understanding the list manipulation functions . . . . . . . . . . . . . . . 101 Using Haskell to manipulate lists . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Using Python to manipulate lists . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Understanding the Dictionary and Set Alternatives . . . . . . . . . . . . . . .103 Using dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Using sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Considering the Use of Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Understanding the uses for strings . . . . . . . . . . . . . . . . . . . . . . . . . 105 Performing string-related tasks in Haskell . . . . . . . . . . . . . . . . . . . . 106 Performing string-related tasks in Python . . . . . . . . . . . . . . . . . . . .106 PART 3: MAKING FUNCTIONAL PROGRAMMING PRACTICAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 CHAPTER 7: Performing Pattern Matching . . . . . . . . . . . . . . . . . . . . . . . . 111 Looking for Patterns in Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Understanding Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Defining special characters using escapes . . . . . . . . . . . . . . . . . . . 114 Defining wildcard characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Working with anchors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115 Delineating subexpressions using grouping constructs . . . . . . . . 116 Using Pattern Matching in Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Working with Pattern Matching in Haskell . . . . . . . . . . . . . . . . . . . . . . . 118 Performing simple Posix matches . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Matching a telephone number with Haskell . . . . . . . . . . . . . . . . . . 120 Working with Pattern Matching in Python . . . . . . . . . . . . . . . . . . . . . . . 121 Performing simple Python matches . . . . . . . . . . . . . . . . . . . . . . . . .121 Doing more than matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Matching a telephone number with Python . . . . . . . . . . . . . . . . . . 124 Table of Contents v CHAPTER 8: Using Recursive Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Performing Tasks More than Once . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126 Defining the need for repetition . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Using recursion instead of looping . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Understanding Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .128 Considering basic recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129 Performing tasks using lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .131 Upgrading to set and dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Considering the use of collections . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Using Recursion on Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Working with Haskell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Working with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Passing Functions Instead of Variables . . . . . . . . . . . . . . . . . . . . . . . . . 137 Understanding when you need a function . . . . . . . . . . . . . . . . . . . 138 Passing functions in Haskell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138 Passing functions in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Defining Common Recursion Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Forgetting an ending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Passing data incorrectly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Defining a correct base instruction . . . . . . . . . . . . . . . . . . . . . . . . . . 141 CHAPTER 9: Advancing with Higher-Order Functions . . . . . . . . . . . 143 Considering Types of Data Manipulation . . . . . . . . . . . . . . . . . . . . . . . .144 Performing Slicing and Dicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Keeping datasets controlled . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Focusing on specific data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Slicing and dicing with Haskell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147 Slicing and dicing with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150 Mapping Your Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Understanding the purpose of mapping . . . . . . . . . . . . . . . . . . . . . 151 Performing mapping tasks with Haskell . . . . . . . . . . . . . . . . . . . . . 152 Performing mapping tasks with Python . . . . . . . . . . . . . . . . . . . . . . 153 Filtering Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Understanding the purpose of filtering . . . . . . . . . . . . . . . . . . . . . . 154 Using Haskell to filter data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Using Python to filter data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Organizing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .157 Considering the types of organization . . . . . . . . . . . . . . . . . . . . . . . 157 Sorting data with Haskell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Sorting data with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 CHAPTER 10: Dealing with Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Developing Basic Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Understanding the functional perception of type . . . . . . . . . . . . . 162 Considering the type signature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Creating types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 vi Functional Programming For Dummies Composing Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 Understanding monoids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 Considering the use of Nothing, Maybe, and Just . . . . . . . . . . . . . .174 Understanding semigroups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .176 Parameterizing Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Dealing with Missing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Handling nulls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Performing data replacement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Considering statistical measures . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Creating and Using Type Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 PART 4: INTERACTING IN VARIOUS WAYS . . . . . . . . . . . . . . . . . . 183 CHAPTER 11: Performing Basic I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Understanding the Essentials of I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . .186 Understanding I/O side effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Using monads for I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Interacting with the user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Working with devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Manipulating I/O Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 Using the Jupyter Notebook Magic Functions . . . . . . . . . . . . . . . . . . . .192 Receiving and Sending I/O with Haskell . . . . . . . . . . . . . . . . . . . . . . . . .195 Using monad sequencing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .195 Employing monad functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 CHAPTER 12: Handling the Command Line . . . . . . . . . . . . . . . . . . . . . . . . . 197 Getting Input from the Command Line . . . . . . . . . . . . . . . . . . . . . . . . . 198 Automating the command line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Considering the use of prompts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Using the command line effectively . . . . . . . . . . . . . . . . . . . . . . . . . 199 Accessing the Command Line in Haskell . . . . . . . . . . . . . . . . . . . . . . . . 200 Using the Haskell environment directly . . . . . . . . . . . . . . . . . . . . . . 200 Making sense of the variety of packages . . . . . . . . . . . . . . . . . . . . . 201 Obtaining CmdArgs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 Getting a simple command line in Haskell . . . . . . . . . . . . . . . . . . . 204 Accessing the Command Line in Python . . . . . . . . . . . . . . . . . . . . . . . . 205 Using the Python environment directly . . . . . . . . . . . . . . . . . . . . . . 205 Interacting with Argparse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 CHAPTER 13: Dealing with Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Understanding How Local Files are Stored . . . . . . . . . . . . . . . . . . . . . . 208 Ensuring Access to Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Interacting with Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Creating new files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 Opening existing files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Table of Contents vii Manipulating File Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Considering CRUD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Reading data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Updating data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Completing File-related Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 CHAPTER 14: Working with Binary Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Comparing Binary to Textual Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Using Binary Data in Data Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Understanding the Binary Data Format . . . . . . . . . . . . . . . . . . . . . . . . . 222 Working with Binary Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .225 Interacting with Binary Data in Haskell . . . . . . . . . . . . . . . . . . . . . . . . . 225 Writing binary data using Haskell . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 Reading binary data using Haskell . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Interacting with Binary Data in Python . . . . . . . . . . . . . . . . . . . . . . . . . 228 Writing binary data using Python . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 Reading binary data using Python . . . . . . . . . . . . . . . . . . . . . . . . . . 229 CHAPTER 15: Dealing with Common Datasets . . . . . . . . . . . . . . . . . . . . . 231 Understanding the Need for Standard Datasets . . . . . . . . . . . . . . . . . 232 Finding the Right Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Locating general dataset information . . . . . . . . . . . . . . . . . . . . . . . 233 Using library-specific datasets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 Loading a Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 Working with toy datasets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 Creating custom data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Fetching common datasets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Manipulating Dataset Entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Determining the dataset content . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Creating a DataFrame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .243 Accessing specific records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 PART 5: PERFORMING SIMPLE ERROR TRAPPING . . . . . . . . . 247 CHAPTER 16: Handling Errors in Haskell . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Defining a Bug in Haskell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Considering recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Understanding laziness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 Using unsafe functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Considering implementation-specific issues . . . . . . . . . . . . . . . . . . 253 Understanding the Haskell-Related Errors . . . . . . . . . . . . . . . . . . . . . . 253 Fixing Haskell Errors Quickly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 Relying on standard debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 Understanding errors versus exceptions . . . . . . . . . . . . . . . . . . . . . 258 viii Functional Programming For Dummies

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.