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:

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 lambda expression and branch our logic as follows:

isGreaterThan 18 7 |> (fun outcome -> 
    match outcome with
        | (true, diff) -> printfn "Difference: %i" diff
        | (false, diff) -> printfn "Less then")

The tuple from isGreaterThan is fed into the lambda expression which is declared with the fun keyword. “Fun” is followed by a parameter “outcome” which will be populated with the result from the isGreaterThan function. The name of this lambda expression parameter is of course arbitrary. It is then pattern matched against the two possible outcomes based on the boolean return value in the tuple.

The above program prints “Difference: 11” in the interactive window.

View all F# related articles here.


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

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

  1. Pingback: F# Weekly #22, 2017 with 2017 F# survey results – Sergey Tihon's Blog

  2. In case you want to shorten it more, you can use `function` instead of `fun outcome -> match outcome with`

  3. giuliohome says:

    You can just to

    match isGreaterThan 18 7 with
    | (true, diff) -> printfn “Difference: %i” diff
    | (false, diff) -> printfn “Less then”

    the lambda expression is not really needed: what’s the reason behind it?

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


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: