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.

Here’s how to rewrite the above function into a lambda function:

let someOfThree = (fun x y z -> x + y + z) 10 20 30

The fun keyword is followed by the input parameters x, y and z. Then comes the arrow delimiter -> followed by the short function body which is only a return statement. We finally have the arguments into the lambda function. This is obviously no different from a normal function call:

let someOfThreeAlt = addThreeNumbers 10 20 30

The difference is only that “addThreeNumbers” was transformed into an anonymous function. Otherwise anonymous functions behave exactly as normal functions with input parameters, a method body and a return statement.

We can also declare the input types:

let someOfThree = (fun (x:int) (y:int) (z:int) -> x + y + z) 10 20 30

Here’s another example to find the absolute value of the difference between two numbers:

let absoluteValue = (fun (x:int) (y:int) -> x - y |> abs) 20 10

View all F# related articles here.

Advertisements

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

2 Responses to Lambda expressions in F#

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

  2. Pingback: The week in .NET – Open XML SDK, Adventure Time – Site Design

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

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

My goal with this blog is to offend everyone in the world at least once with my words… so no one has a reason to have a heightened sense of themselves. We are all ignorant, we are all found wanting, we are all bad people sometimes.

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: