Booleans and Comparisons
Prerequisites: Data types
Comparison operators
printfn "%b" (5 = 5) // equal
printfn "%b" (5 <> 3) // not equal
printfn "%b" (5 > 3)
printfn "%b" (5 <= 3)
printfn "%b" ("apple" < "banana")
printfn "%d" (compare 2 7)
true
true
true
false
true
-1
F# uses = for equality and <> for "not equal" — not == and !=.
compare returns a negative number, zero, or a positive number.
Structural equality
= compares values, not references, for lists, tuples, arrays,
records, and other F# types:
printfn "%b" ([1; 2; 3] = [1; 2; 3])
printfn "%b" ((1, "a") = (1, "a"))
printfn "%b" ([| 1; 2 |] = [| 1; 2 |])
printfn "%b" ([1; 2] < [1; 3])
true
true
true
true
Logical operators
let age = 25
let hasTicket = true
printfn "%b" (age >= 18 && hasTicket)
printfn "%b" (age < 12 || age > 65)
printfn "%b" (not hasTicket)
true
false
false
&& and || short-circuit: the right side is only evaluated if it
can change the answer. not is a function, not a ! operator.
Only bool is bool
There's no "truthiness": if 1 then ... or if name then ... is a
compile error. Write the comparison you mean — if count > 0 or
if name <> "".
- Use
=,<>,<,>,<=,>=;comparefor sorting logic. =is structural for F# types — two lists with the same items are equal.