F# from the ground up
Values that never change, and functions that fit together
- Beginner
- 12 chapters
- 211 min read
Learn F# by writing it: values and printfn, let bindings that cannot be assigned to, functions applied one argument at a time, pipes and composition, lists walked with map and filter, folds and sorts and groups, tuples and pattern matching, records with named fields, discriminated unions with a match that must be complete, options in place of null, strings and format strings, and a sales report at the end that uses all of it.
What You'll Learn
- Write a script as a list of lines and print with printfn and its placeholders
- Tell int, float, string, bool and char apart, and print any of them with %A
- Define a value with let and know that nothing can change it afterwards
- Use a function as a value, with partial application and a type annotation where one is needed
- Turn nested calls into a pipeline with |> and name a pipeline with >>
- Walk a list with List.map and List.filter instead of writing a loop
- Boil a list down with List.fold, and reach for sumBy, sortBy, countBy and groupBy first
- Group values as tuples and take them apart in a let or a match
- Choose between cases with match, and order the specific patterns before the broad one
- Declare a record, create one with its field names, and copy one with `with`
- Model a fixed set of cases with a discriminated union and handle every case
- Answer a search that can fail with option, Option.map and defaultValue
- Join and split text with String.concat and Split, and align columns in a format string
- Build a report from records, a sort, a group and a set of formatted lines
Prerequisites
- No programming experience needed — the first chapter starts at the first value
- If you know another language, the biggest difference is that nothing here is assigned to twice
Course Content
- 01Values, types and printfnNot started
What the page runs when you press Run, the five kinds of value the rest of the course is built from, and the format string that turns any of them into a line of output.
- 02let bindings that never changeNot started
A name in F# is a definition rather than a slot: there is nothing to assign to, so a value you read at line three still means the same thing at line thirty.
- 03Functions and partial applicationNot started
A function is a value with parameters, written without brackets and called without commas. Give it fewer arguments than it takes and the answer is another function.
- 04Pipes and compositionNot started
Two small operators do most of the arranging in F#: `|>` sends a value into a function, and `>>` joins two functions into one that can be used later.
- 05Lists and the helpers that walk themNot started
A list is one type repeated, written with semicolons, and `List.map` and `List.filter` replace most loops: change every value, or keep the ones that pass a test.
- 06Fold, sort and groupNot started
A fold boils a list down to one answer, `sumBy` and `maxBy` are the folds you will use most, and `sortBy`, `countBy` and `groupBy` turn a list into the sections of a report.
- 07Tuples and pattern matchingNot started
A tuple groups values by position, and `match` takes a value apart by shape. Together they replace a pile of `if`s with a list of cases the compiler can check.
- 08Records: your own named dataNot started
A record gives each part of a value a name and a type. You create one with its field names, copy one to change a field, and both operations leave the original exactly as it was.
- 09Discriminated unions and matchNot started
A union says a value is one of a fixed set of cases, each carrying its own data. `match` takes it apart, and the compiler knows when a case has been forgotten.
- 10Option instead of nullNot started
A search that can fail answers `Some value` or `None`, and the type makes you deal with both. No null, no exception, no book that is not there.
- 11Text, splitting and format stringsNot started
Strings join with `+`, split into arrays and come back together with `String.concat`. A format string with widths is what turns values into columns a reader can scan.
- 12Capstone: a sales report from recordsNot started
Everything in the course in one script: a record, a list of them, a sort by one field, a group by another, and formatted lines printed by the function that builds them.
