F# · Lesson 1 · On the house

Values and printing

let bindings, immutability, printfn format specifiers, and running scripts with dotnet fsi. Lesson 1 is free — on the house.

Lessons · F# · Lesson 1 of 10 · On the house

Values and printing

let bindings, immutability, printfn format specifiers, and running scripts with dotnet fsi.

Illustration for F#
Lesson 1 is on the house.

F# runs on .NET. Save code as hello.fsx and run it with dotnet fsi hello.fsx, or type into the dotnet fsi REPL (end each entry with ;;).

let x = 5 binds a name to a value. Bindings are immutable: x <- 6 is an error unless declared let mutable x = 5.

printfn is type-checked: %d int, %f float (%.2f two decimals), %s string, %b bool, %A any value in F# syntax.

String interpolation: $"{name} is {age}", or typed $"%d{age}".

Indentation is significant: a block's lines must line up; tabs are not allowed.

printfn

let name = "Ada"
let year = 1843
printfn "%s wrote notes in %d" name year
printfn "%.2f" 3.14159
printfn "%A" [1; 2; 3]
let mutable count = 0
count <- count + 1
printfn $"{name} count={count}"
Match this when you type.

Quiz

How do you change a value declared with plain let?

Quiz

Which operator assigns to a mutable binding?

Quiz

Which printfn specifier prints any value in F# syntax?

Quiz

What does printfn "%.2f" 3.14159 print?

Check

Match the still for: Values and printing.

Open-book: the answers are on this page. Pass every quiz and check (5) to mark the lesson done. This visit: 0/5.