Cover Page

Title Page

Functional Programming For Dummies®

To view this book's Cheat Sheet, simply go to www.dummies.com and search for “Functional Programming For Dummies Cheat Sheet” in the Search box.

Introduction

The functional programming paradigm is a framework that expresses a particular set of assumptions, relies on particular ways of thinking through problems, and uses particular methodologies to solve those problems. Some people view this paradigm as being akin to performing mental gymnastics. Other people see functional programming as the most logical and easiest method for coding any particular problem ever invented. Where you appear in this rather broad range of perspectives depends partly on your programming background, partly on the manner in which you think through problems, and partly on the problem you’re trying to solve.

Functional Programming For Dummies doesn’t try to tell you that the functional programming paradigm will solve every problem, but it does help you understand that functional programming can solve a great many problems with fewer errors, less code, and a reduction in development time. Most important, it helps you understand the difference in the thought process that using the functional programming paradigm involves. Of course, the key is knowing when functional programming is the best option, and that’s what you take away from this book. Not only do you see how to perform functional programming with both pure (Haskell) and impure (Python) languages, but you also gain insights into when functional programming is the best solution.

About This Book

Functional Programming For Dummies begins by describing what a paradigm is and how the functional programming paradigm differs. Many developers today don’t really understand that different paradigms can truly change the manner in which you view a problem domain, thereby making some problem domains considerably easier to deal with. As part of considering the functional programming paradigm, you install two languages: Haskell (a pure functional language) and Python (an impure functional language). Of course, part of this process is to see how pure and impure languages differ and determine the advantages and disadvantages of each.

Part of working in the functional programming environment is to understand and use lambda calculus, which is part of the basis on which functional programming it built. Imagine that you’re in a room with some of the luminaries of computer science and they’re trying to decide how best to solve problems in computer science at a time when the term computer science doesn’t even exist. For that matter, no one has even defined what it means to compute. Even though functional programming might seem new to many people, it’s based on real science created by the best minds the world has ever seen to address particularly difficult problems. This science uses lambda calculus as a basis, so an explanation of this particularly difficult topic is essential.

After you understand the basis of the functional programming paradigm and have installed tools that you can use to see it work, it’s time to create some example code. This book starts with some relatively simple examples that you might find in other books that use other programming paradigms so that you compare them and see how functional programming actually differs. You then move on to other sorts of programming problems that begin to emphasize the benefits of functional programming in a stronger way. To make absorbing the concepts of functional programming even easier, this book uses the following conventions:

  • Text that you’re meant to type just as it appears in the book is bold. The exception is when you’re working through a step list: Because each step is bold, the text to type is not bold.
  • Because functional programming will likely seem strange to many of you, I’ve made a special effort to define terms, even some of those that you might already know, because they may have a different meaning in the functional realm. You see the terms in italics, followed by their definition.
  • When you see words in italics as part of a typing sequence, you need to replace that value with something that works for you. For example, if you see “Type Your Name and press Enter,” you need to replace Your Name with your actual name.
  • Web addresses and programming code appear in monofont. If you’re reading a digital version of this book on a device connected to the Internet, note that you can click the web address to visit that website, like this: www.dummies.com.
  • When you need to type command sequences, you see them separated by a special arrow, like this: File ⇒ New File. In this case, you go to the File menu first and then select the New File entry on that menu. The result is that you see a new file created.

Foolish Assumptions

You might find it difficult to believe that I’ve assumed anything about you — after all, I haven’t even met you yet! Although most assumptions are indeed foolish, I made these assumptions to provide a starting point for the book.

You need to be familiar with the platform that you want to use because the book doesn’t provide any guidance in this regard. To give you maximum information about the functional programming paradigm, this book doesn’t discuss any platform-specific issues. You need to know how to install applications, use applications, and generally work with your chosen platform before you begin working with this book. Chapter 2 does show how to install Python, and Chapter 3 shows how to install Haskell. Part 2 of the book gives you the essential introduction to functional programming, and you really need to read it thoroughly to obtain the maximum benefit from this book.

This book also assumes that you can find things on the Internet. Sprinkled throughout are numerous references to online material that will enhance your learning experience. However, these added sources are useful only if you actually find and use them.

Icons Used in This Book

As you read this book, you see icons in the margins that indicate material of interest (or not, as the case may be). This section briefly describes each icon in this book.

Tip Tips are nice because they help you save time or perform some task without a lot of extra work. The tips in this book are time-saving techniques or pointers to resources that you should try in order to get the maximum benefit from Python, Haskell, or the functional programming paradigm.

Warning I don’t want to sound like an angry parent or some kind of maniac, but you should avoid doing anything marked with a Warning icon. Otherwise, you could find that your program serves only to confuse users, who will then refuse to work with it.

Technicalstuff Whenever you see this icon, think advanced tip or technique. You might find these tidbits of useful information just too boring for words, or they could contain the solution that you need to get a program running. Skip these bits of information whenever you like.

Remember If you don’t get anything else out of a particular chapter or section, remember the material marked by this icon. This text usually contains an essential process or a bit of information that you must know to write Python, Haskell, or functional programming applications successfully.

Beyond the Book

This book isn’t the end of your functional programming experience — it’s really just the beginning. I provide online content to make this book more flexible and better able to meet your needs. That way, as I receive email from you, I can do things like address questions and tell you how updates to Python, its associated packages, Haskell, it’s associated libraries, or changes to functional programming techniques that affect book content. In fact, you gain access to all these cool additions:

  • Cheat sheet: You remember using crib notes in school to make a better mark on a test, don’t you? You do? Well, a cheat sheet is sort of like that. It provides you with some special notes about tasks that you can do with Python or Haskell that not every other developer knows. In addition, you find some quick notes about functional programming paradigm differences. You can find the cheat sheet for this book by going to www.dummies.com and searching this book's title. Scroll down the page until you find a link to the Cheat Sheet.
  • Updates: Sometimes changes happen. For example, I might not have seen an upcoming change when I looked into my crystal ball during the writing of this book. In the past, that simply meant the book would become outdated and less useful, but you can now find updates to the book by searching this book's title at www.dummies.com.

    In addition to these updates, check out the blog posts with answers to reader questions and demonstrations of useful book-related techniques at http://blog.johnmuellerbooks.com/.

  • Companion files: Hey! Who really wants to type all the code in the book? Most readers would prefer to spend their time actually working through coding examples, rather than typing. Fortunately for you, the source code is available for download, so all you need to do is read the book to learn functional programming techniques. Each of the book examples even tells you precisely which example project to use. You can find these files at www.dummies.com. Click More about This Book and, on the page that appears, scroll down the page to the set of tabs. Click the Downloads tab to find the downloadable example files.

Where to Go from Here

It’s time to start your functional programming paradigm adventure! If you’re a complete functional programming novice, you should start with Chapter 1 and progress through the book at a pace that allows you to absorb as much of the material as possible.

If you’re a novice who’s in an absolute rush to get going with functional programming techniques as quickly as possible, you can skip to Chapter 2, followed by Chapter 3, with the understanding that you may find some topics a bit confusing later. You must install both Python and Haskell to have any hope of getting something useful out of this book, so unless you have both languages installed, skipping these two chapters will likely mean considerable problems later.

Readers who have some exposure to functional programming and already have both Python and Haskell installed can skip to Part 2 of the book. Even with some functional programming experience, Chapter 5 is a must-read chapter because it provides the basis for all other discussions in the book. The best idea is to at least skim all of Part 2.

If you’re absolutely certain that you understand both functional programming paradigm basics and how lambda calculus fits into the picture, you can skip to Part 3 with the understanding that you may not see the relevance of some examples. The examples build on each other so that you gain a full appreciation of what makes the functional programming paradigm different, so try not to skip any of the examples, even if they seem somewhat simplistic.

Part 1

Getting Started with Functional Programming

IN THIS PART …

Discover the functional programming paradigm.

Understand how functional programming differs.

Obtain and install Python.

Obtain and install Haskell.