Real World Functional Programming

Real-World Functional Programming

Real-World Functional programming is a best-selling introduction to functional concepts. The book demonstrates the key functional concepts on real-world examples using C#, teaching F# alogside. You can get the book from Manning.com, from Amazon.com or Amazon.co.uk or your favorite book store.

Real World Functional Programming

Reviews and quotes


Thanks to everyone who wrote a review or posted a comment about the Real-World Functional Programming book. Thanks to the Manning Early Access program, the book was available to the readers even before it was completed, which gave us some very valuable feedback.

Blogs and Online Magazines

Real-World Functional Programming is a book by Tomas Petricek and Jon Skeet. It’s terrific. It succeeds not just as an introduction to functional programming and F#, but also as a simply excellent programming text. [...]

Readers will not only learn the ideas, but also get trained to see applications for them, as well as acquiring knowledge of the tools. This book is my candidate for the best programming book of 2010.

Overall, this is an amazingly good book that puts into words the many benefits of the functional paradigm, and shows that the F# programming language can embrace both the functional and object-oriented styles of programming. C# programmers, too, can gain something from this book, using it to understand the ideas behind LINQ and lambda expressions and why these have been incorporated into the language. I’m going to be re-reading it and working through the examples in more depth in the near future.

I really enjoyed reading this book – it’s my first real foray into the world of functional programming since university and I think I understand the functional approach to programming much better than I did back then from reading this book. [...]I’d certainly recommend this to any .NET developers curious about learning how to apply ideas derived from functional programming to their C# code and indeed to any developers looking to start out learning about functional programming.

Real-World Functional Programming teaches a paradigm using a functional language and not the other way around. There are better books to learn all of the ins and outs of F# but it would be just like reading a book on C# without understanding OOP (Object Oriented Programming).

So if you’re a software developer that’s looking how to improve your ability – even if you’re not interested in F#, hack even if you’re not even a .NET developer – read this book!

It’s one of the rare cases which left me with a headache, the pleasant kind of which is left after learning something new. It is set in the .Net world, but I can only recommend it to everyone, as it offers a remarkably well written way into functional programming.

From the book cover

You will never look at your code in the same way again!

Mads Torgersen, C# PM Microsoft

.NET needs more functional programmers…this book shows you how to become one.

Stuart Caborn, Lead Consultant Thoughtworks

Warning: this book has a very high Wow! factor. It made my head hurt…in a good way!

Mark Seemann, Independent Developer/Architect

I recommend it to all software crafts people, not just .NET developers.

Paul King, Director, ASERT

A truly functional book!

Andrew Siemer, .NET Architect

From Amazon Reviews

A hallmark of this book is a very pragmatic, Rosetta stone
approach to F#.

Andre M. Van Meulebrouck

This book goes out of the way to make C# developers feel comfortable with the new .NET language. I would recommend this book to anyone who is trying to get out of the same old boring stuff and move on to something truly innovative and exciting!

John Mclintire

Better still, it’s a terrific resource for helping convince the rest of your team of the advantages of adopting a functional approach and F# as a language. I can think of few better reasons for getting this book immediately!

Ryan Riley “panesofglass”

Samples and related free material


Sample chapters

Are you curious about the book and want to see how it looks like? The following two chapters are available as free samples. They are nice demonstration of the mix of theory and practice that you'll find in the book:

  • Chapter 4: Exploring F# and .NET libraries by example shows a first real-world application implemented in F#. It uses various .NET and F# libraries to implement a program for drawing pie charts. You’ll also see how to efficiently use the tools F# provides during the development process.
  • Chapter 12: Sequence expressions and alternative workflows The chapter starts by showing how we can work with collections of data using. We’ll introduce F# sequence expressions, which are designed for this purpose and look how this feature relates to LINQ. However, you’ll also see that this isn’t a built-in feature unlike its closest counterpart in C# – it’s an example of a more general ability to change what code means that is called computation expressions (or monads).

Real-World Functional Programming on MSDN

The chapter I below of the material is based on chapters 1, 2 and 3 of the book. The remaining chapters are newly written material that focuses on real-world examples of using F#.

Chapter I: Introducing Functional Programming (source code)

This chapter explains the basic concepts behind functional programming and the F# language. Many of the concepts are demonstrated in C# to guide programmers without prior experience with F# or functional programming.

The chapter explains concepts such as immutability, first-class functions and expression-based programming. It shows the benefits of functional programming that follow from these concepts – such as the fact that they make testing and reasoning about programs easier.

Chapter II: Server-Side Functional Programming (source code)

The shift towards the software as a service means that an increasing number of applications need to be written as servers. This chapter explain server-side development and agent-based concurrency in F#.

The chapter introduces F# agents (the MailboxProcessor type). It includes a tutorial that develops a simple chat server using agents, encapsulates it into a reusable .NET object and exposes it as a web page using the HttpListener type. The chapter also includes two reusable agents that solve common data processing tasks.

Chapter III: Developing Client-Side Applications (source code)

A lot of code assumes that it’s in the driving seat—that it controls what happens at each step. This model breaks down for user interfaces. This chapter explain how to write reactive UIs in F# using Silverlight.

The chapter explains two approaches to writing reactive applications in F#. The first approach is declarative and it composes data-flow using event combinators. The second approach is more imperative and it describes control-flow using asynchronous workflows. The chapter also covers practical aspects, such as how to use F# with XAML.

Chapter IV: Numerical and Symbolic Computing (source code)

Thanks to its scientific roots, F# is a perfect langauge for numerical computing. This chapter explain how to use .NET libraries such as Math.NET Numerics, Sho, and F# Power Pack to implement numerical algorithms in F#.

The chapter explains what makes F# a great language for numerical computing. Then it introduces various numerical .NET libraries that can be used from F#. It presents a large number of examples using Microsoft Sho and Math.NET Numerics. (This chapter was written by Yin Zhu.)

Chapter V: Working with Data (source code)

Most of web applications work with data in one way or another. F# is suitable for developing a data-processing parts of web applications. This chapter explains how to use Data and Web technologies from F#.

The chapter uses F# to develop the model and controllers of an ASP.NET MVC web application with a front-end created using C# and Razor. It shows how to use asynchronous workflows to write more efficient web pages and how to easily access SQL database using dynamic operator.

Chapter VI: Visualizing Data with .NET Charts (source code)

This chapter explains how to create charts in F# using the .NET Chart Controls library, F# wrapper for the library named FSharpChart and other technologies such as Excel.

The chapter describes .NET Chart Controls library (including topics like data-binding) as well as the FSharpChart wrapper that has been designed specifically for F#. It includes numerous larger tutorials, such as visualization of financial data, and How To articles that show how to create common chart types.

Source code

The full source code for all the examples in the book is available for download here. Have a look at some of the examples to see if the book is interesting for you!

Chapter excerpts

The following articles were published as chapter excerpts. The articles are based on early versions of the book, so they may contain more bugs, but they present some important features of the book.

  • Programming user interfaces using F# workflows
    (dotnetslackers.com)

    F# asynchronous workflows can be used to solve a wide range of programming problems. In this article we’ll look how to use asynchronous workflows for elegantly expressing the control flow of interaction with the user. We’ll also look at clear functional way for encoding drag&drop-like algorithm.

  • Tracking dependencies and side-effects using immutability
    (codeproject.com)

    The Functional Programming for the Real World book explains essential concepts of the functional programming paradigm using examples in C# 3.0 and F#. In this article we look at one very important advantage of using one of the core functional concepts – immutability.

  • Immutable data structures in C# and F#
    (codeproject.com)

    The Real World Functional Programming book explains essential concepts of this paradigm using examples in C# 3.0 and F#. In this article we look at immutability, which stands behind the clarity of functional programs.

  • Declarative programming style
    (codeguru.com)

    In this article, I’ll talk about two properties of functional languages that together define the overall style of writing functional code – recursion and higher-order functions. These two concepts together define what a declarative programming style is, so after discussing them, we’ll talk about the benefits of this style.

  • Functional Programming in .NET using C# and F#
    (Manning Greenpaper)

    This article looks at several aspects of FP and how the same concepts, which are essential for the functional paradigm, look in the F# and in C# 3.0 with LINQ. We will shortly look at the basic programming language features like lambda functions and type inference that are now available in both F# and C#.

Errata


I tried to minimize the number of errors in the book as hard as I could. However, some errors are (unfortunately!) inevitable. This page contains the list of known issues. If you spot an issue that isn’t listed on this page, please send me an email at tomas@tomasp.net.

Chapter 3

  • Page 65, first paragraph There is a typo in the following sentence: “Rather than adding the method to the Type<T1, T2> class from listing 3.7, we implement it as an extension method.” The name of the class isn’t Type<T1, T2>, but should instead be Tuple<T1, T2>.
  • Page 68, Section 3.3, second paragraph There is a confusing grammar error. The sentence “The type that represents functional lists is either an empty or is composed …” should be “The type that represents functional lists is either an empty list or is composed …”.
  • Page 78, Listing 3.18 The pattern hd::tail should instead be head::tail, because the code uses the head name later on:
    match list with
    | [] -> init
    | head::tail ->
        let resultRest = aggregateList op init tail
        op resultRest head

Chapter 4

  • Page 83, Listing 4.2 On the third line of the listing I use a function List.ofseq, which is a wrong name. The correct name of the function is List.ofSeq (with uppercase “S”). The line should look like this:

    let cells = List.ofSeq(csvLines.Split(','))
  • Page 101, Listing 4.11 When calling MeasureString, we give it lbl value as the first argument, but this value isn’t declared anywhere. It should be the title parameter instead:

    let size = gr.MeasureString(title, fnt)

Chapter 5

  • Page 105 (introduction for Part 2): There is a repeated sentence: “In part 2 we’ll review all the basic functional features that make it possible to write the first version of the program.”
  • There is an additional word “them” in the last sentence of section 5.4.3. “We’ll talk about generic functions in detail them later, but for now…”
  • Page 139 There is a wrong bullet on the third line from the end of the page “...when specifying the first argument for List.map (5)”. The bullet number should be (4)

Chapter 6

  • Page 155 The last line of the second inline code example is missing a closing parenthesis “)”. It is the example that shows how to use map function instead of the inner matchparameter instead. The line should be:

    | Some(first) -> readInput() |> Option.map (fun second -> first + second)
  • Page 159 In Listing 6.15, in the left column, the second line is match opt with, but it should be match input with.

Chapter 9

  • Page 241 The last line on the page says “We can break the >NET rules when…”. The correct version should be of course “.NET”.
  • Page 248, Listing 9.14 The second line from the end of the listing shows a wrong F# Interactive output. The listing shows Client: John Doe, q=16, but the actual output should be Client: John Doe, loan coefficient: 16
  • Page 251, second paragraph A sentence in the paragrpah says “At this point, you can choose which style to follow: an imperative one with some mutable types, or a purely functional one where everything is mutable.” The emphasized word should be immutable.
  • Page 257, first paragraph A description of the previous listing: “The next step is to invoke the Deflate method (1)” refers mistakenly to the bullet (1). It should refer to the bullet (2) instead.

Chapter 10

  • Page 272, Listing 10.9 In the body of the map function, the last line (map' f list []) should be indented one level less (to match with the let rec definition).
  • Page 276, first paragraph below listing 10.12 A description says “… which is similar to List.int that we discussed in listing 10.2.” The List.int should be List.init.

Chapter 11

  • Page 311, Listing 11.22 The text below the listing says “To specify what should be displayed, we set the DataMember property to the name…”. The name of the property should be DisplayMember (the listing itself is correct).

Chapter 12

  • Page 323, last paragraph There is an additional “is” word in the sentence: “We do this is because the sequence of random colors….. “

Chapter 13

  • Page 369, second paragraph from the end In the sentence “In this section, we’ve seen how to get a list of regions that we want to further study.” the last two words should be switched, so that it reads better.
  • Page 380, Listing 13.9 The last two lines of the listing use incorrect names of values (declared earlier). The names should be names and dataArray instead of namesVert and tableArr. The correct version is:

    worksheet.Range("B3", "B" + endColumn).Value2

Chapter 14

  • Page 387, Listing 14.3 There is a mismatch in the C# and F# version of the code that is compared in the listing. The F# version counts primes from 1,000,000 to 3,000,000, while the C# version only between 1,000,000 and 2,000,000.

Chapter 15

  • Page 456, Listing 15.24 The type signature of the trade function in the F# Interactive output is val trade : int -> string -> Contract. The two input arguments are swapped and the correct type signature is val trade : string -> int -> Contract.

Chapter 16

  • Page 477, first paragraph There are redundant words in the first sentence: “All we have to do is to add the following single line before the line that last line that runs the recursion”. The redundant bit should be deleted.

   Go back to the top