# Functional Programming For Dummies

Your guide to the functional programming paradigm

Functional programming mainly sees use in math computations, including those used in Artificial Intelligence and gaming. This programming paradigm makes algorithms used for math calculations easier to understand and provides a concise method of coding algorithms by people who aren't developers. Current books on the market have a significant learning curve because they're written for developers, by developers--until now.

Functional Programming for Dummies explores the differences between the pure (as represented by the Haskell language) and impure (as represented by the Python language) approaches to functional programming for readers just like you. The pure approach is best suited to researchers who have no desire to create production code but do need to test algorithms fully and demonstrate their usefulness to peers. The impure approach is best suited to production environments because it's possible to mix coding paradigms in a single application to produce a result more quickly. Functional Programming For Dummies uses this two-pronged approach to give you an all-in-one approach to a coding methodology that can otherwise be hard to grasp.

* Learn pure and impure when it comes to coding

* Dive into the processes that most functional programmers use to derive, analyze and prove the worth of algorithms

* Benefit from examples that are provided in both Python and Haskell

* Glean the expertise of an expert author who has written some of the market-leading programming books to date

If you're ready to massage data to understand how things work in new ways, you've come to the right place!

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

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 63

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

Performing Reduction Operations 85

Considering alpha-conversion 85

Considering ß-reduction 86

Considering eta-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

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

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

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

Chapter 17: Handling Errors in Python 259

Defining a Bug in Python 260

Considering the sources of errors 260

Considering version differences 262

Understanding the Python-Related Errors 263

Dealing with late binding closures 263

Using a variable 264

Working with third-party libraries 264

Fixing Python Errors Quickly 265

Understanding the built-in exceptions 265

Obtaining a list of exception arguments 266

Considering functional style exception handling 267

Part 6: The Part of Tens 269

Chapter 18: Ten Must-Have Haskell Libraries 271

binary 271

Hascore 273

vect 273

vector 274

aeson 274

attoparsec 275

bytestring 275

stringsearch 276

text 276

moo 277

Chapter 19: Ten (Plus) Must-Have Python Packages 279

Gensim 280

PyAudio 281

PyQtGraph 282

TkInter 283

PrettyTable 283

SQLAlchemy 284

Toolz 284

Cloudera Oryx 285

funcy 285

SciPy 286

XGBoost 287

Chapter 20: Ten Occupation Areas that Use Functional Programming 289

Starting with Traditional Development 289

Going with New Development 290

Creating Your Own Development 291

Finding a Forward-Thinking Business 292

Doing Something Really Interesting 292

Developing Deep Learning Applications 293

Writing Low-Level Code 293

Helping Others in the Health Care Arena 294

Working as a Data Scientist 294

Researching the Next Big Thing 295

Index 297