Declaring a type as a Tuple in F#

We saw how Tuples work in F# in this post. Tuples are containers for various elements such as this one:

let myFirstTuple = (45, "hello world", 5., true, addThreeNumbers)

If you’d like to declare a type to be of Tuple then you can use the ‘*’ character as follows:

Read more of this post

Enumerations and pattern matching in F#

Enumeration types in F# are declared using the pipe symbol. Each element will have an integer value. Here’s an example with the first 6 months of the year:

type MonthsInFirstHalf = January = 1 | February = 2 | March = 3 | April = 4 | May = 5 | June = 6

We select an element using the dot notation which is the same as in C# or Java:

let april = MonthsInFirstHalf.April

The more interesting language construct is the F# equivalent of the switch statements to check for the value of the enumeration. In F# it’s called pattern matching.

Read more of this post

Declaring generic types in F#

Generic types have type parameters in F#. This is no surprise to those coming from other languages like Java and C#. Generics increases the flexibility of objects by letting one or more of its properties take on multiple types.

In F# type parameters are declared using the single quote followed by the type name like here:

type Container<'a> = {description: string; containedValue: 'a}

Read more of this post

Equality checking in F#

F# uses the equality sign ‘=’ to check for equality like here:

let areTheyEqual = 5 = 5

areTheyEqual will evaluate to true. At first it can be confusing to see the two single equality signs like that. The first one is the assignment operator and the second one checks for equality. It might be easier to rewrite the above as follows:

let areTheyEqual = (5 = 5)

The equality operator in F# checks for value equality for value types. It’s easy to check for equality on lists, arrays, tuples etc. Here are a couple of examples:

Read more of this post

Creating list ranges in F#

We can easily create a list of integers in F# with a start and end value as follows:

let myRangeList = [1..10]

myRangeList will contain the integers from 1 to 10:

int list = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]

Read more of this post

Tuples in F#

Tuples are collections where each element can be of different type. If you have a C# or Python background then you must be familiar with tuples. A tuple can be used to return multiple values from a function without having to declare an object for it.

Here’s a tuple with 5 elements:

let addThreeNumbers x y z = x + y + z
let myFirstTuple = (45, "hello world", 5., true, addThreeNumbers)

This tuple has an integer, a string, a float, a boolean and a function in it.

val myFirstTuple : int * string * float * bool * (int -> int -> int -> int) = (45, “hello world”, 5.0, true, )

Read more of this post

For-each loops in F#

For-each loops in F# are declared using the keywords for, in and do.

Here’s how to iterate through a list of integers:

let myList = [1..10]
let loopTester = 
    for i in myList do
        printfn "Current value: %i" i

Executing the function loopTester gives the following output:

Current value: 1
Current value: 2
Current value: 3
Current value: 4
Current value: 5
Current value: 6
Current value: 7
Current value: 8
Current value: 9
Current value: 10

Read more of this post

ultimatemindsettoday

A great WordPress.com site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

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: