You are previewing Programming F#.

Programming F#

Cover of Programming F# by Chris Smith Published by O'Reilly Media, Inc.
O'Reilly logo

Chapter 3. Functional Programming

With the basics out of the way, you can begin to examine F# from the approach of a particular style. This chapter is devoted to F#’s main paradigm: functional programming. In a nutshell, functional programming is about being more declarative in your code. In imperative programming (covered in Chapter 4), you spend your time listing out the specific steps to perform a task. In functional programming, you specify what is to be done, but not how. Even though functional programming is no silver bullet, the result is that programs are much clearer, and some problems like concurrency and parallel programming are solved much more easily.

Functional programming isn’t going to replace imperative or object-oriented programming on its own; rather, it just provides a different approach to use so that in certain applications you can be much more productive.

For a language to be considered “functional,” it typically needs to support a few key features:

  • Immutable data

  • Ability to compose functions

  • Functions can be treated as data

  • Lazy evaluation

  • Pattern matching

We will go into each of these raw, functional concepts and what they offer throughout the chapter. By the end you will be able to write purely functional code, and leverage the elegance and simplicity of declarative programming. A deeper look at functional concepts, such as tail recursion and closures, will come in Chapter 7.

Programming with Functions

The heart of functional programming is thinking about code in ...

The best content for your career. Discover unlimited learning on demand for around $1/day.