Damien Gonot
Home Blog Notes About

F#

Homepage / Notes / Computer Science / Programming Languages / F#

Language Features

Basics

Type inference

"Hello, World!"
val it: string = "Hello, World!"
let myInt = 9
val myInt: int = 9

Lists

Commas are never user as delimiters, only semicolons

let firstList = [1; 2; 3]
val firstList: int list = [1; 2; 3]
[1..5]
val it: int list = [1; 2; 3; 4; 5]

List Comprehensions

[for i in 1..10 do i * i]
val it: int list = [1; 4; 9; 16; 25; 36; 49; 64; 81; 100]

Functions

let square x = x * x
square 4
val square: x: int -> int
val it: int = 16
let add x y = x + y
add 2 3
val add: x: int -> y: int -> int
val it: int = 5

Anonymous Functions

List.map (fun x -> x * x) [1; 2; 3]
val it: int list = [1; 4; 9]

Pattern Matching

let x = "a"
match x with
 | "a" -> "x is a"
 | "b" -> "x is b"
 | _ -> "x is neither"
val x: string = "a"
val it: string = "x is a"

Pipe Operator

firstList |> List.map square
val it: int list = [1; 4; 9]
firstList |> List.map square |> List.sum
val it: int = 14

Backwards Pipe

List.sum <| [1; 2; 3; 4]
val it: int = 10

Resources

Announcing F# 8

List of F# 8 new features: https://devblogs.microsoft.com/dotnet/announcing-fsharp-8/