Most of the programming you have done so far has been in the functional style. Although this has enabled you to write succinct, powerful programs, you never quite used the functional style to its full potential. Functional programming means more than just treating functions as values. Embracing the functional programming paradigm and having it help shape your thought process will enable you to write programs that would otherwise be prohibitively difficult in an imperative style.
In this chapter, we build on what you learned about functional
programming back in Chapter 3, and introduce
new language features that help you to be more productive in the functional
style. For example, using active patterns allows you to increase the power
of your pattern matching and eliminate the need for
when guards, and by creating auto-opened modules,
you can extend common F# modules.
In addition, we look at some of the more mind-bending aspects of functional programming. You will learn how to use advanced forms of recursion to avoid stack overflows and write more efficient programs using lists. We also take a look at some common design patterns and data structures for functional code.
To begin, let’s look at how to take pattern matching from “switch statements on steroids” to an entirely new level.
Pattern matching adds power to your programming by giving you a way to be
much more expressive in code branching than using
if expressions alone. It ...