Values in F#

F# has various value types including strings, integers, floats, booleans, etc. Here are a few basic examples.

open System

[<EntryPoint>]
let main argv =
    // Strings, which can be concatenated with the '+' operator.
    printfn "%s" ("f" + "sharp")

    // Integers and floats.
    printfn "1+1 = %d" (1 + 1)
    printfn "7.0/3.0 = %f" (7.0 / 3.0)

    // Booleans, with boolean operators as you'd expect.
    printfn "%b" (true && false)
    printfn "%b" (true || false)
    printfn "%b" (not true)

    0 // return an integer exit code

To run this program, save it as values.fs and use the F# compiler (fsc) to compile it, then run the resulting executable:

$ fsharpc values.fs
$ mono values.exe
fsharp
1+1 = 2
7.0/3.0 = 2.333333
false
true
false

In this F# version:

  1. We use printfn for formatted printing, which is similar to printf in other languages.
  2. String concatenation is done with the + operator, just like in the original example.
  3. Arithmetic operations are straightforward and similar to other languages.
  4. Boolean operations use && for AND, || for OR, and not for negation.
  5. The main function in F# needs to return an integer exit code, which we’ve set to 0.

F# is a functional-first language, so while this procedural style works, F# code often leverages functional programming concepts for more idiomatic and concise expressions.