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#

Beginning F#

Thu 04 April 2019

First steps Let's learn F#!

Part of the F# Functions 101 series

F# is an incredibly easy language to pick up!

Not really, even though it should.

There are several hurdles on the way of the beginner. First of all, a really good book aimed at beginners. This is vital in a language plagued by TIMTOWTDIs.

F# is a functional language built on top of .NET, a strongly Object Oriented framework. This causes a lot of impedance between the language and the libraries.

It's like a Jedi and a Sith being siamese twins!

F# is not alone here. The same situation arises with Swift and Cocoa, although I feel like it's been better handled in the F# / .Net case.

One of the annoyances of having an evil siamese twin, is the curse of the TIMTOWTDI: Having a gazillion ways of doing the same thing, without a guide to which one is best, at least for newcomers, is very confusing.

There are some books...

Expert F# is horribly disorganized, keeps jumping around concepts and has the obnoxious habit of mentioned things that were not explained earlier.

It might be a great reference, but it's certainly not good learning material.

The F# book is also very frustrating. A lengthy dissertation of all the TIMTOADIs without much guidance at all: you order a steak and the waiter instead goes on to explain all the different cuts in a cow. Dude, I just want a steak!!!

F# Applied is pretty good, but not aimed at beginners.

DDD made functional by Scott Wlaschin is awesome: provides guidance and promotes great design, but again, it's not for beginners...

Put my blog where my mouth is

I'll try to alleviate this entry barrier by posting my notes as I learn the language. Join me and let's learn an awesome language tha can be used for backends, Data Science, web frontends and even mobile apps! How cool is that?

First things first

We will start by the most basic building block: functions.

Other posts in this series

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