Feeding a function result into a pattern matching function 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:

let isGreaterThan x y =     
    if x > y then (true, x - y)
    else (false, 0)
    
let (gt, diff) = isGreaterThan 10 6
printfn "Is greater: %b, diff: %i" gt diff

…which prints “Is greater: true, diff: 4” in the interactive window. We can feed the result into a pattern matching expression and branch our logic as follows:

isGreaterThan 19 6 |> function
    | (true, diff) -> printfn "Difference: %i" diff
    | (false, diff) -> printfn "Less then"

The pattern matching expression is introduced by the function keyword followed by pattern matching logic. The above program prints “Difference: 13” in the interactive window.

The pattern matching branches must be exhaustive so the following would not be sufficient:

isGreaterThan 19 6 |> function
    | (true, diff) -> printfn "Difference: %i" diff

The code still runs but we get a warning: Incomplete pattern matches on this expression. For example, the value ‘(false,_)’ may indicate a case not covered by the pattern(s).

We get an exception in case there’s no matching pattern branch:

isGreaterThan 6 19 |> function
    | (true, diff) -> printfn "Difference: %i" diff

Microsoft.FSharp.Core.MatchFailureException: The match cases were incomplete

View all F# related articles here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

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

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: