O'Reilly logo

Programming F# 3.0, 2nd Edition by Chris Smith

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 (which we cover 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 made much easier.

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 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, comes later in Chapter 7.

Understanding Functions

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required