Learning F# – Part 2

You can become a serverless blackbelt. Enrol to my 4-week online workshop Production-Ready Serverless and gain hands-on experience building something from scratch using serverless technologies. At the end of the workshop, you should have a broader view of the challenges you will face as your serverless architecture matures and expands. You should also have a firm grasp on when serverless is a good fit for your system as well as common pitfalls you need to avoid. Sign up now and get 15% discount with the code yanprs15!

Disclaimer: I do not claim credit for the code examples and much of the contents here, these are mostly extracts from the book by Chris Smith, Programming F#: A comprehensive guide for writing simple code to solve complex problems. In fact, if you’re thinking of learning F# and like what you read here, you should buy the book yourself, it’s easy to read and the author has gone go great lengths to keep things simple and included a lot of code examples for you to try out yourself.

Primitive Types

F# is statically typed, meaning that type checking is done at compile time.

F# supports the full set of primitive .Net types which are built into the F# language and separate from user-defined types.

Here’s a table of all the numeric types (both integer and floating-point) with their suffixes:


F# also allows you to specify values in hexadecimal (base 16), octal (base 8 ) or binary (base 2) using prefix 0x, 0o, or 0b:


There are no implicit type conversion in F#, which eliminates subtle bugs introduced by implicit type conversion as can be found in other languages.

Arithmetic Operators

You can use standard arithmetic operators on numeric primitives, like other CLR-based languages, integer division rounds down to the next lowest number discarding the remainder. Here’s a table of all supported operators:


A very important to note here is that by default, these arithmetic operators do not check for overflow! If a number becomes too big for its type it’ll overflow to be negative, and vice versa:


F# also features all the standard math functions, here’s a table of the common math functions:



If you are dealing with data larger than 2^64, F# has the BigInt type for representing arbitrarily large integers. While the BigInt type is simply an alias for the System.Numerics.BigInteger type, it’s worth noting that neither C# nor VB.Net has syntax to support arbitrarily large integers.

BigInt uses the I suffix for literals, see example below:


You should remember that although BigInt is heavily optimized, it is still much slower than using the primitive integer types.

Bitwise Operations

Primitive integer types support bitwise operators for manipulating values at a binary level:



The .Net platform is based on Unicode, so characters are represented using 2-byte UTF-16 characters. To define a character value, you can put any Unicode character in single quotes, for example:


Like C#, to represent special control characters you need to use an escape sequence from the table below:


You can get the byte value of a character literal by adding a B suffix:



String literals are defined by enclosing a series of characters in double quotes which can span multiple lines. To access a character from within a string, use the indexer syntax, .[ ], and pass in a zero-based character index. For example:


If you want to specify a long string, you can break it up across multiple lines using a single backslash, \, for example:


Like in C#, you can define a verbatim string using the @ symbol, which ignores any escape sequence characters:


Boolean Values

F# has the bool type (System.Boolean) as well as standard Boolean operators listed below:


F# uses short-circuit evaluation when evaluating Boolean expressions, meaning that if a result can be determined after evaluating the first of the two expressions, the second value won’t be evaluated. For example:

true || f() – will evaluate to true without executing function f.

false && g() – will evaluate to false without executing function g.

Comparison and Equality

You can compare numeric values using standard operators listed below:


All these operators evaluate to a Boolean value except the compare function which returns -1, 0, or 1 depending on whether the first parameter is less than, equal to, or greater than the second.

You should have noticed that these operators are similar to those found in SQL Server and F# doesn’t distinguish assignment from equality (like C#, where = is assignment and == is equality comparison).

When it comes to equality, as in other CLR-based languages, it can mean different things – value equality or referential equality. For value types, equality means the values are identical. For reference types, equality is determined by overriding the System.Object method Equals.

Liked this article? Support me on Patreon and get direct help from me via a private Slack channel or 1-2-1 mentoring.
Subscribe to my newsletter

Hi, I’m Yan. I’m an AWS Serverless Hero and I help companies go faster for less by adopting serverless technologies successfully.

Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.

Hire me.

Skill up your serverless game with this hands-on workshop.

My 4-week Production-Ready Serverless online workshop is back!

This course takes you through building a production-ready serverless web application from testing, deployment, security, all the way through to observability. The motivation for this course is to give you hands-on experience building something with serverless technologies while giving you a broader view of the challenges you will face as the architecture matures and expands.

We will start at the basics and give you a firm introduction to Lambda and all the relevant concepts and service features (including the latest announcements in 2020). And then gradually ramping up and cover a wide array of topics such as API security, testing strategies, CI/CD, secret management, and operational best practices for monitoring and troubleshooting.

If you enrol now you can also get 15% OFF with the promo code “yanprs15”.

Enrol now and SAVE 15%.

Check out my new podcast Real-World Serverless where I talk with engineers who are building amazing things with serverless technologies and discuss the real-world use cases and challenges they face. If you’re interested in what people are actually doing with serverless and what it’s really like to be working with serverless day-to-day, then this is the podcast for you.

Check out my new course, Learn you some Lambda best practice for great good! In this course, you will learn best practices for working with AWS Lambda in terms of performance, cost, security, scalability, resilience and observability. We will also cover latest features from re:Invent 2019 such as Provisioned Concurrency and Lambda Destinations. Enrol now and start learning!

Check out my video course, Complete Guide to AWS Step Functions. In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. There is something for everyone from beginners to more advanced users looking for design patterns and best practices. Enrol now and start learning!