Check out my new course Learn you some Lambda best practice for great good! and learn the best practices for performance, cost, security, resilience, observability and scalability.
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.
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.
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.
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:
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.
I specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.
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.
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. Enrol now and enjoy a special preorder price of £9.99 (~$13).
Are you working with Serverless and looking for expert training to level-up your skills? Or are you looking for a solid foundation to start from? Look no further, register for my Production-Ready Serverless workshop to learn how to build production-grade Serverless applications!
Here is a complete list of all my posts on serverless and AWS Lambda. In the meantime, here are a few of my most popular blog posts.
- Lambda optimization tip – enable HTTP keep-alive
- You are thinking about serverless costs all wrong
- Many faced threats to Serverless security
- We can do better than percentile latencies
- I’m afraid you’re thinking about AWS Lambda cold starts all wrong
- Yubl’s road to Serverless
- AWS Lambda – should you have few monolithic functions or many single-purposed functions?
- AWS Lambda – compare coldstart time with different languages, memory and code sizes
- Guys, we’re doing pagination wrong