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.
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
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
#help;; for help
Please note the use of
;; to tell the REPL to execute any previously-typed commands.
This is the code for a simple console project, that prints "Hello, World!" to STDOUT, and exits with an exit code of
[<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 called
mainwith a single parameter
argv. Because this is the program entry point,
argvwill 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!"- the
printfnfunction 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. Putting
0as 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.
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 http://fsharp.org/use/windows/ for more options.
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.
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 http://fsharp.org/use/linux/ for more options.