The Book of F# - Review and Interview with Dave Fancher
In the book, Fancher discusses the role of Visual Studio in F# and the various scripts which can be used in the Sandbox and F# interactive consoles. He continues the discussion with comprehensive coverage of various concepts such as bindings, core data types, enumerations, flow control, generics, string formatting, use of comments, and the steps involved in handling exceptions.
Fancher provides a detailed overview of all the concepts associated with curried functions, partial application, pipelining, function composition, classes, structures, inheritance, interfaces, custom operators, object expressions, recursive functions, lambda expressions, sequences, arrays, lists, sets and maps.
In chapter 7, you will learn about pattern expressions and the aspects involved with matching nulls, tuples, records and collections besides coverage of partial active and parameterized active patterns with the help of code snippets.
Some of the other topics covered in the book include the following
- Static measure testing
- Guidelines for composing measure formulas
- Use of measures
- Static conversion factors and functions
- Writing functions using Generic measures
- Quoted expression creation using .NET Reflection
- Parsing quoted expressions
- Usage of type providers
Towards the end of the book, Fancher provides detailed coverage of Asynchronous and Parallel programming with special focus on Task Parallel Library besides a chapter devoted to computation expressions with the help of sample applications.
The Book of F# will be very useful for developers who wish to learn the concepts associated with F# and can also be used as a ready reference guide.
InfoQ had a chat with Fancher, who is also a Microsoft MVP, to know more about F# and the book.
InfoQ: Dave - You demonstrate Reverse Polish Notation (RPN) calculator application instead of traditional Hello World. Why did you choose this example?
The traditional Hello World example is an easy path to instant gratification. I agree that it’s nice for new programmers to experience the sense of accomplishment of having the computer do what they just told it to do but The Book of F# isn’t for new programmers – it’s for experienced programmers looking to jump into functional programming. In that regard, a Hello World example doesn’t really show the audience anything practical. I opted to first demonstrate the Reverse Polish Notation calculator because it exposes the reader to a number of F# features including first-class functions, closures, pattern matching, bindings, and module functions without using much code.
InfoQ: In Chapter 3, under Core Data Types section you talk about type abbreviations and type inference. Is there any difference between these two terms?
Yes. Type abbreviations are aliases for known types. For instance, System.String is abbreviated as string and System.Int32 is abbreviated as int. Type inference is the compiler’s ability to determine the correct type from a given value. The F# compiler is often able to infer the correct type for a binding or parameter but sometimes it needs some help. In those cases, we use type annotations, which may include an explicit type name or type abbreviation.
InfoQ: What is the role of generics in F#?
Generics are an important part of F#’s type system. They play much the same role in F# as in the traditional .NET languages but a key way that F# distinguishes itself from traditional .NET languages is that its type inference system often attempts to automatically generalize parameters. That is, the compiler will automatically make a function generic if it recognizes that a function isn’t dependent upon any of its arguments being of a particular concrete type. The compiler can also detect certain types of constraints that must be applied to the generic type parameters, making the code robust while retaining type safety. This feature often frees us from having to define numerous overloads or use code in ways which we didn’t initially anticipate without a refactoring effort.
InfoQ: What advantages does F# have over C#? Are there particularly scenarios where you would favor one over the other?
At a high level, the advantages F# has over C# are its terse syntax, powerful type inference, and its emphasis on functional programming. Speaking to specific language features though, features such as curried functions, record types, discriminated unions, pattern matching, integrated units of measure, and object expressions make it very difficult for me to recommend using C# over F#. At the risk of being cliché, the only time I might consider favoring C# over F# on a new application is for the parts that need to use a framework (ASP.NET MVC, for instance) where the Visual Studio tooling doesn’t yet support F#. Even then, it’s not impossible to use those frameworks, it just requires a bit more manual effort to connect the pieces that Visual Studio would do for you in a C# project.
InfoQ: F# and Haskell are closely related. What do you see as the many differences?
I’m not as familiar with Haskell as I should be to answer this adequately but one well known difference is that Haskell is a pure functional language whereas F# is impure. What this basically means is that Haskell doesn’t allow functions to have side-effects (except in controlled situations) while F# does.
InfoQ: In Chapter 9, you talk about .NET Reflection. What role does it play in the development of F# applications?
I brought up .NET reflection in Chapter 9 to discuss how to generate quoted expressions from code outside of your control. Reflection plays the same role in F# that it does in the traditional .NET languages.
InfoQ: Can you share with us the usefulness of Asynchronous Programming Model (APM) and its advantages?
As I state at the beginning of Chapter 11, the Asynchronous Programming Model has long been the preferred approach for asynchronous programming in .NET but F# offers several alternatives such as asynchronous workflows and agent-based programming. These approaches make asynchronous programming more accessible and maintainable by abstracting away the details of thread creation, synchronization, and callbacks.
InfoQ: What are the main features of the Task Parallel Library?
Though not an F# feature, per se, the Task Parallel Library makes it easy to scale CPU-bound operations by taking available machine resources into account. Some of the key features are that it is task-based (as opposed to thread-based), easy cancellation handling, and easy continuation handling.
InfoQ: Can you share with us how Asynchronous Workflows benefit F#?
In some regards, Asynchronous workflows are to I/O-bound operations what the Task Parallel Library is to CPU-bound operations. Asynchronous workflows make it easy to handle potentially expensive operations such as manipulating files or making network requests without blocking UI threads (or other parent threads). What sets asynchronous workflows apart from more traditional approaches is that they use familiar keywords to implicitly perform asynchronous operations and handle continuations. What’s more is that they use the same mechanism for cancellations as the Task Parallel Library so it’s easy to cancel them without writing lots of code.
InfoQ: What is the main advantage of using Computation expressions in F#?
From sequence expressions to query expressions to asynchronous workflows, computation expressions play an important role in F# development. Behind the scenes, computation expressions are syntactic sugar over builder classes. At compile time, the computation expression is “de-sugared” such that each nested expression is converted to the appropriate method call on the builder class. This model makes it possible for developers to define their own builder classes and, through the associated computation expressions, repurpose a number of language elements so complex operations appear to be a natural part of F#.
InfoQ: What other resource you suggest to learn F# apart from your book?
There are a number of great resources for learning F#. The ones I typically refer people to are:
About the Book Author
Dave Fancher has been developing software with the .NET Framework for more than a decade. He is a familiar face in the Indiana development community as both a speaker and participant in user groups around the state. In July 2013, Dave was recognized as a Microsoft MVP (Most Valuable Professional) for Visual F#. When not writing code or writing about code at davefancher.com, he can often be found watching a movie or gaming on his Xbox One.
Great Read So Far
The Book of F# has been great because I come from an OO world in C#, Java, C++ and a few other languages, and we all know that Function Programming provides a paradigm shift when it comes to code, and The Book of F# reads as a sort of guide for OO developers and architects to safely maneuver through this shift in programming. I recommend it to any .NET developers looking to find new ways of solving problems.
Mike Hartington Jul 26, 2015