Tutorial: Functional Programming in C# and F#

Applies to: Functional Programming

Authors: Tomas Petricek and Jon Skeet

Referenced Image

Get this book in Print, PDF, ePub and Kindle at manning.com. Use code “MSDN37b” to save 37%.

Summary: This tutorial demonstrates the basics of functional programming by showing how to work with the two most important functional data types in C# and F#. Seeing the C# and F# versions side by side makes the tutorial easy to follow for experienced C# developers.

This topic contains the following sections.

  • Introducing Immutable Types in .NET
  • Related Topics
  • See Also

This article is an excerpt from Real World Functional Programming: With Examples in F# and C# by Tomas Petricek with Jon Skeet from Manning Publications (ISBN 9781933988924, copyright Manning Publications 2009, all rights reserved). No part of these chapters may be reproduced, stored in a retrieval system, or transmitted in any form or by any means—electronic, electrostatic, mechanical, photocopying, recording, or otherwise—without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews.

Introducing Immutable Types in .NET

One of the properties of functional programs is that they work with immutable data structures. This tutorial walks you through a series of examples that introduce this style of programming. It starts by using tuples, which are the simplest immutable type and are already available in .NET. Then, it looks at working with lists, which is one of the core F# types. To demonstrate how lists work, the tutorial shows how to implement them in C#.

The tutorial is divided into two steps. You are advised to start with tuples and then move on to lists. The first step also explains features like pattern matching that are needed in the second step.

Title

Description

Step 1: Working with Tuples

The first part of the tutorial that introduces basic immutable types looks at tuples. Tuples make it possible to combine a known number of values of possibly different types into a single value and are readily available in both C# and F#.

Step 2: Working with Functional Lists

The second part of the tutorial looks at lists. Lists can be used for storing an arbitrary number of values of the same type. The tutorial explores how to use them in F# and how to implement the same functionality in C#.

The following articles discuss immutability in broader terms. They look at the benefits of immutability and, in particular, how it makes programs more readable:

See Also

This article is based on Real World Functional Programming: With Examples in F# and C#. Chapters related to the content of this article are:

  • Book Chapter 3: “Meet tuples, lists, and functions in F# and C#” is an extended version of this tutorial. It shows type declarations for tuples and lists in larger detail and discusses how to write reusable code using functions.

  • Book Chapter 4: “Exploring F# and .NET libraries by example” shows how to use concepts from this tutorial to implement a simple chart drawing using .NET libraries.

The following MSDN documents are related to the topic of this article:

  • Tuple(T1, T2) Class is a reference for a class that implements two-element .NET tuples that are used by F# and can be accessed from C#.

  • Tuples (F#) contains more information about programming with F# tuples.

  • Records (F#) are similar to tuples, but individual components have labels.

  • Lists (F#) contains more information about programming with F# lists.

To download the code snippets shown in this article, go to https://code.msdn.microsoft.com/Chapter-1-Introducing-F-c967460d

Previous article: Overview: Understanding Functional Types

Next article: Step 1: Working with Tuples