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

Records in F#

Records in F# are similar to objects in OOP languages. They can have named fields and other members. However, like many other elements in F#, they are immutable.

Here’s how to declare a record of type Book with a couple of fields and a function:

type Book = {
    title: string;
    numberOfPages:int;
    author: string
} 
with member this.takesLongTimeToRead = this.numberOfPages > 500

Here’s how we can declare a new Book:

Read more of this post

Arrays in F#

In F# we declare an array using square brackets, pipes and semi-colons as follows. Here’s how to declare an integer array with some elements:

let myArray = [|1;3;5;7;9|]

int [] = [|1; 3; 5; 7; 9|]

Read more of this post

Relational operators in F#

The relational operators in F# don’t behave in exactly the same way as they do in popular OOP languages like C# or Java.

The greater-than and less-than operators are the same, here’s an example:

let value = 5
let trueOrFalseGt = value > 5
let trueOrFalseLt = value < 5

The boolean values will be false as expected:

val trueOrFalseGt : bool = false
val trueOrFalseLt : bool = false

Read more of this post

Creating lists in F#

Lists in F# are type safe and immutable. Type safety means that all elements in the list must be of the same type. Immutability implies that once the list is created we cannot add new elements to it. Instead, a new list will be created with the elements of the old list copied to the new list along with the new value(s) we want to add to it.

Here’s how to declare an empty list:

let myFirstList = [];

The ‘::’ operator is used to add a new item to the head of the list:

Read more of this post

Creating substrings in F#

Reading a chunk of a string into another string is based on the position of the characters in the string. The characters in the string form an array of chars and we can read parts of it by referring to the required array positions of the characters. This is no different from popular OOP languages like C# and Java. The only difference is the syntax.

Let’s start with the following string:

let longString = "The message couples the distressed heat before a purchase."

The random sentence generator site has a lot of similar meaningless sentences for testing.

The most simplistic application of substrings is to read one character:¨

Read more of this post

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

ARCHIVED: Bite-size insight on Cyber Security for the not too technical.