## Feeding a function result into a pattern matching lambda expression in F#

Say we have an F# function that returns a tuple of two elements:

```let isGreaterThan x y =
if x > y then (true, x - y)
else (false, 0)
```

…, i.e. we return true and the difference between the two input integers if the first integer is greater. Otherwise we return a false and a 0. Here’s how we can consume this function:

## Lambda expressions in F#

Lambda expressions in F# are inline anonymous functions, i.e. functions without a name. They are typically short and concise functions that are not meant to be reused.

Here’s a normal F# function that adds three numbers:

```let addThreeNumbers x y z = x + y + z
```

This is a conventional named function that can be invoked from other parts of the application. We can convert it to a lambda function using the “fun” keyword. However, it must be invoked in place.

## More complex pattern matching in F#

Pattern matching branches in F# can be more advanced using the when keyword. Consider the following list matching function:

```let listMatcher (l:list<'a>) =
match l with
| _ when l.IsEmpty -> "This is an empty list"
| _ when l.Length > 10 -> "This is a large list"
| _ when l.Length < 10 -> "This is a small list"
| _ -> l |> List.map (sprintf "%A") |> String.concat ","
```

## Basic pattern matching in F#

Pattern matching in F# is somewhat similar to switch blocks in C#. However, pattern matching blocks can provide much more complex branching logic than switch blocks.

Consider the following F# function:

```let isGreaterThan x y =
if x > y then (true, x - y)
else (false, 0)
```

…, i.e. we return true and the difference between the two input integers if the first integer is greater. Otherwise we return a false and a 0. In other words the return type is a tuple with two elements, a boolean and an integer.

## Implementing a C# interface with an F# object expression

Suppose you have the following C# interface ICalculator with 4 functions:

```namespace Project.Domains
{
public interface ICalculator
{
int Subtract(int x, int y);
float Divide(float x, float y);
int Multiply(int x, int y);
}
}
```

## Implementing a C# interface with an F# type

Suppose you have the following C# interface ICalculator with 4 functions:

```namespace Project.Domains
{
public interface ICalculator
{
int Subtract(int x, int y);
float Divide(float x, float y);
int Multiply(int x, int y);
}
}
```

We can implement this interface in an F# type as follows:

## Consuming a C# class in F#

F# and C# can work together pretty easily. Say that your domain classes are contained in a C# class library called Project.Domains. Let’s take the following Product class as an example:

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project.Domains
{
public class Product
{
public Product(string name, int price)
{
Name = name;
Price = price;
}

public string Name { get; }
public int Price { get; }

public double CalculateDiscountedPrice(double percentageOff)
{
double discount = Price * percentageOff;
return Price - discount;
}
}
}
```

## Filtering exceptions on exception messages in F#

Here’s an example of pattern matching in a with clause in F#:

```let testDotNetExceptions =
try
raise (new System.IndexOutOfRangeException("Your list is not large enough"))
with
| 😕 System.IndexOutOfRangeException as ex ->
printfn "Out of range: %s" ex.Message
0
| 😕 System.Exception as ex ->
printfn "Something bad has happened: %s" ex.Message
0
```

## Handling .NET exceptions in F#

When working with .NET languages from F# it can happen that the F# code needs to deal with .NET exceptions from the System namespace. The way to deal with those exceptions is about the same as in the case of F# exceptions but pattern matching in the with clause is slightly different:

## Custom exceptions in F#

We can build custom exceptions in F# with the exception keyword. We can also assign multiple parameters to it in a tuple:

```exception ProductNotFoundException of string * int * int
```
Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.