If you want to try F# but without the project setup it requires, then you might try one these which are easier:
Try F# using the online dotnet compiler. Don't forget to choose F# from the language dropdown on the left-hand side.
To follow the rest of this guide, you will need to install dotnet cli.
You can write F# code interactively by using repl. The repl can be started by running
dotnet fsi in the command-line. Each statement you write should be terminated with double semicolons. To exit the repl, type Ctrl+D.
Typing directly into the repl can be hard, that is why F# lets you edit your code in a
.fsx file with your favorite editor and load it in the repl afterwards.
To do this, run
dotnet fsi --use:<filename> (Don't include < >).
You could also use the arrow button in the top right-hand corner to send the whole file to the repl.
If you get
fsi not found error, you can fix it by enabling
Fsharp: Use Sdk Scripts in your Vscode settings.
To import some
.fsx file into your other
.fsxfile, you can use
#load directive. For example, if you want to load
// B.fsx let rec calculateFuel mass = let fuel = (mass / 3) - 2 if fuel > 0 then fuel + calculateFuel fuel else 0
// A.fsx #load "B.fsx" // this loads B.fsx file // Now you can either B.calculateFuel 100 // or open B calculateFuel 100
Simple testing can be done manually via the repl. In the event you need to do several tests after each change, you might write your tests in another
.fsx file. For Example, this is how I tested my code for day1 puzzle of advent of Code 2019
// Day01.fsx let calculateFuel1 mass = (mass / 3) - 2 let rec calculateFuel2 mass = let fuel = (mass / 3) - 2 if fuel > 0 then fuel + calculateFuel2 fuel else 0
// Day01Test.fsx #load "../Day01.fsx" open Day01 let ``calculateFuel1: mass of 12`` = (calculateFuel1 12) = 2 let ``calculateFuel1: mass of 14`` = (calculateFuel1 14) = 2 let ``calculateFuel1: mass of 1969`` = (calculateFuel1 1969) = 654 let ``calculateFuel1: mass of 100756`` = (calculateFuel1 100756) = 33583 let ``calculateFuel2: mass of 14`` = (calculateFuel2 14) = 2 let ``calculateFuel2: mass of 1969`` = (calculateFuel2 1969) = 966 let ``calculateFuel2: mass of 100756`` = (calculateFuel2 100756) = 50346
Next, we load our test file in the repl. Our tests pass when all bindings in our test file evaluate to true. Notice how F# let us write descriptive names using double backticks.
F# for fun and profit is a comprehensive site for everything F# related. It also hosts many F# talks. Two videos which I recommend are Functional programming design patterns and Domain Modeling Made Functional.
F# collections docs I use this whenever I need to find a function such as map, scan, fold, pairwise, etc for operating on collections (List, Map, Seq, Set, etc).
Coursera's Programming Languages, Part A. This course teaches functional programming using standard ML, a language that shares many similarities with F#.