4 F# Fundamentals

 

This chapter covers

  • Understanding the difference between expressions and statements
  • Working with immutable data

This chapter discusses two of the core features of F# that will have a large effect on how you write and design code: Expressions and immutability. Both features go hand-in-hand: without one, the other wouldn’t be especially useful. Both are designed to change the way you write code into one that’s oriented around working with values and applying transformations to those values as the basic mechanism of implementing any logic. That might sound complicated but don’t worry; it’s not.

4.1 Expressions

Expressions and statements are two sides of the same coin. They’re sometimes described in all sorts of complicated or vague ways but in F# it’s quite clear, as table 4.1 shows.

Table 4.1 Expressions vs. statements (view table figure)

 

Returns something?

Has side-effects?

Expressions

Always

Occasionally

Statements

Never

Always

4.1.1 Purity and side effects

4.1.2 Difficulties with statements

4.1.3 Expressions to the rescue

4.1.4 Expressions in F#

4.1.5 Composability

4.1.6 Unit

4.1.7 Ignore

4.2 Immutable data

4.2.1 The problem with mutability

4.2.2 Modeling with mutable and immutable data

4.2.3 Optimizing and opinionated languages