Fernando Rodriguez's blog

Code Long & Prosper

2 - Let expressions in F#

Thu 04 April 2019

Part of the F# Functions 101 series

the importance of a name

Like the pine trees lining the winding road

I got a name, I got a name

Like the singing bird and the croaking toad

I got a name, I got a name

I Got a Name

let the fun begin

When learning F#, there are two things you should grasp as quickly as possible: the let keyword, and functions. Both are essential for grokking the language.

let is by far the most important keyword you will learn. It's syntax is deceptively simple, and it has 2 main roles in F#:

Number 1: let creates bindings

A binding gives a name to a value (if you come from an OOP language, then whenever I type value, feel free to read object).

Some examples of values are:

42 // an integer
"functional programming is fun" // a string
[1;3;34;-58]  // a list of ints

Using let to create bindings is as follows:

let answer = 42
let motto = "functional programming is fun"
let entries = [1;3;34;-58]

The body of the let expression (the right hand side), can be much longer and have several lines. This is where the second role comes in:

Number 2: let creates scopes

Scopes are sections of your code where certain bindings (names) are valid.

Here's an example:

let z = 
    let a = 4
    let b = 5
    a + b   // This will be the value of z!

The scope is delimited by indentation, just like Python! Within the scope created by let z, a and b are valid identifiers and have a value respectively.

How long can the let expression be?

The compiler will keep evaluating the let body as far as possible (think of a greedy algorithm if you wish). It will stop when the indentation is reduced and will succeed if the last expression returns a value (for example, a + b in the example above). The value of the last expression will be the value of the binding.

If the last expression, does not eval to a value, the compiler will yell at you.

At this point, I would recommend playing around with the interactive FSharp and try creating different let bodies.

Functions are values too!

In the next post, we will learn about the most powerful feature in F# : function abstraction.

In a nutshell, functions are just another type of value within the language and can be treated just like any other value: ints, strings, etc...

Don't let the simplicity of this statement fool you! Within it, lies all the power and flexibility of F#.

You are now staring at an ordinary burrow in the middle of a field. Little do you know that this is the rabit hole, the one that will you take far down, straight into Wonderland!

Would you like to know more?

This post is part of a series on F# Functions 101.

  1. F# Functions 101 index
  2. Let the fun begin: let expressions in F#

1 - Everything no one told you about F# functions

Thu 04 April 2019

Part of the F# Functions 101 series

F# functions 101

First steps

This post is part of a series on F# Functions 101. This should give you a solid understanding of some of the most important concepts a beginner needs to grasp when learning F#:

  • let
  • functions
  • function evaluation
  • pure functions vs fake functions
  • the amazing power of partial evaluation or currying
  • and more!

Would you like to know more?

This post is part of a series on F# Functions 101.

  1. F# Functions 101 index
  2. Let the fun begin: let expressions in F#