Getting started with F#
Remarks#
F# is a “functional-first” language. You can learn about all of the different types of expressions, along with functions.
The F# compiler — which is open source — compiles your programs into IL, which means that you can use F# code from any .NET compatible language such as C#; and run it on Mono, .NET Core, or the .NET Framework on Windows.
Versions#
Version | Release Date |
---|---|
1.x | 2005-05-01 |
2.0 | 2010-04-01 |
3.0 | 2012-08-01 |
3.1 | 2013-10-01 |
4.0 | 2015-07-01 |
Installation or Setup
Windows
If you have Visual Studio (any version including express and community) installed, F# should already be included. Just choose F# as the language when you create a new project. Or see https://fsharp.org/use/windows/ for more options.
OS X
Xamarin Studio supports F#. Alternately, you could use VS Code for OS X, which is a cross-platform editor by Microsoft.
Once done with installing VS Code, launch VS Code Quick Open
(Ctrl+P) then run ext install Ionide-fsharp
You may also consider Visual Studio for Mac.
There are other alternatives described here.
Linux
Install the mono-complete
and fsharp
packages via your distribution’s package manager (Apt, Yum, etc.). For a good editing experience, use either Visual Studio Code and install the ionide-fsharp
plugin, or use Atom and install the ionide-installer
plugin. See https://fsharp.org/use/linux/ for more options.
Hello, World!
This is the code for a simple console project, that prints “Hello, World!” to STDOUT, and exits with an exit code of 0
[<EntryPoint>]
let main argv =
printfn "Hello, World!"
0
Example breakdown Line-by-line:
[<EntryPoint>]
- A .net Attribute that marks “the method that you use to set the entry point” of your program (source).let main argv
- this defines a function calledmain
with a single parameterargv
. Because this is the program entry point,argv
will be an array of strings. The contents of the array are the arguments that were passed to the program when it was executed.printfn "Hello, World!"
- theprintfn
function outputs the string** passed as its first argument, also appending a newline.0
- F# functions always return a value, and the value returned is the result of the last expression in the function. Putting0
as the last line means that the function will always return zero (an integer).
** This is actually not a string even though it looks like one. It’s actually a TextWriterFormat, which optionally allows the usage of statically type checked arguments. But for the purpose of a “hello world” example it can be thought of as being a string.
F# Interactive
F# Interactive, is a REPL environment that lets you execute F# code, one line at a time.
If you have installed Visual Studio with F#, you can run F# Interactive in console by typing "C:\Program Files (x86)\Microsoft SDKs\F#\4.0\Framework\v4.0\Fsi.exe"
. On Linux or OS X, the command is fsharpi
instead, which should be either in /usr/bin
or in /usr/local/bin
depending on how you installed F# — either way, the command should be on your PATH
so you can just type fsharpi
.
Example of F# interactive usage:
> let i = 1 // fsi prompt, declare i
- let j = 2 // declare j
- i+j // compose expression
- ;; // execute commands
val i : int = 1 // fsi output started, this gives the value of i
val j : int = 2 // the value of j
val it : int = 3 // computed expression
> #quit;; //quit fsi
Use #help;;
for help
Please note the use of ;;
to tell the REPL to execute any previously-typed commands.