New LEARN modules: Write your first code in F#, Write your first F# programs

Published Jul 21 2021 03:53 AM 2,217 Views
Microsoft

Screenshot 2021-07-21 at 11.42.54.png

F# is an open-source, cross-platform programming language that makes it easy to write succinct, performant, robust, and practical code.

References

 

 

Why F#?

There are many language features and aspects of the F# language that make it easy to be productive when writing code:

  • Succinct: You write less code with F# that's also expressed in a clear manner.
  • Performant: F# comes with built-in parallelism and concurrency. It also uses the fact that it's part of the .NET runtime to speed things up.
  • Robust: There are language constructs that makes the code fault tolerant and robust like immutable by default, null value management and more.
  • Supports multiple programming paradigms: F# lets you choose the patterns and practices most effective for solving problems by providing strong support for functional and object programming paradigms.

 

How do I get started?

There are a few things you need to get started.

  • .NET SDK. You need to install the .NET SDK to be able to buiild, run, test and also create F# projects. You can find the F# SDK install here
  • Ionide. It's a VS Code plugin. It's not necessary to install, but it will give you things like Intellisense, tooltips, codelens, error highlight and more.

 

Your first code

You can start writing F# by using the REPL called FSI, you can also scaffold an F# project using the dotnet executable.

Create a project

You create a project using dotnet like so:

 

dotnet new console --language F# -o MyFSharpApp

 

 

Then you get a project with files:

  • Program.fs, your entrypoint file
  • MyFSharpApp.csproj, your project file that contains everything it needs to know to build the project like dependencies for example.

What does the code look like?

Here you have the code of Program.fs. Itt contains the functions from() and main(). main() is the entry point of the app, as seen by [<EntryPoint>].

 

open System

// Define a function to construct a message to print
let from whom =
    sprintf "from %s" whom

[<EntryPoint>]
let main argv =
    let message = from "F#" // Call the function
    printfn "Hello world %s" message
    0 // return an integer exit code

 

The from() function takes the arg whom and the function body prints a string using the function sprintf.

 

How do I learn more?

 

%3CLINGO-SUB%20id%3D%22lingo-sub-2569795%22%20slang%3D%22en-US%22%3ENew%20LEARN%20modules%3A%20Write%20your%20first%20code%20in%20F%23%2C%20Write%20your%20first%20F%23%20programs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2569795%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Screenshot%202021-07-21%20at%2011.42.54.png%22%20style%3D%22width%3A%20747px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F297341i627E194D17ACB625%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Screenshot%202021-07-21%20at%2011.42.54.png%22%20alt%3D%22Screenshot%202021-07-21%20at%2011.42.54.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EF%23%20is%20an%20open-source%2C%20cross-platform%20programming%20language%20that%20makes%20it%20easy%20to%20write%20succinct%2C%20performant%2C%20robust%2C%20and%20practical%20code.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--408629840%22%20id%3D%22toc-hId--408629840%22%3E%3CA%20href%3D%22https%3A%2F%2Fdev.to%2Fazure%2Fwrite-your-first-code-in-f-36gl%23%25C2%25A0references%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20name%3D%22%25C2%25A0references%22%3E%3C%2FA%3EReferences%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3ELEARN%20module%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3EThe%20first%20F%23%20module%20of%20many%20was%20just%20published%20on%20LEARN%2C%20check%20it%20out%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-gb%2Flearn%2Fmodules%2Ffsharp-first-steps%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ETake%20your%20first%20steps%20with%20F%23%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3ELEARN%20module%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3EThe%20second%20module%20just%20published.%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-gb%2Flearn%2Fmodules%2Ffsharp-first-program%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EWrite%20your%20first%20F%23%20programs%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Ffsharp%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EF%23%20docs%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH2%20id%3D%22toc-hId-2078882993%22%20id%3D%22toc-hId-2078882993%22%3E%3CA%20href%3D%22https%3A%2F%2Fdev.to%2Fazure%2Fwrite-your-first-code-in-f-36gl%23%25C2%25A0why-f%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20name%3D%22%25C2%25A0why-f%22%3E%3C%2FA%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId-271428530%22%20id%3D%22toc-hId-271428530%22%3EWhy%20F%23%3F%3C%2FH2%3E%0A%3CP%3EThere%20are%20many%20language%20features%20and%20aspects%20of%20the%20F%23%20language%20that%20make%20it%20easy%20to%20be%20productive%20when%20writing%20code%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3ESuccinct%3C%2FSTRONG%3E%3A%20You%20write%20less%20code%20with%20F%23%20that's%20also%20expressed%20in%20a%20clear%20manner.%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EPerformant%3C%2FSTRONG%3E%3A%20F%23%20comes%20with%20built-in%20parallelism%20and%20concurrency.%20It%20also%20uses%20the%20fact%20that%20it's%20part%20of%20the%20.NET%20runtime%20to%20speed%20things%20up.%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3ERobust%3C%2FSTRONG%3E%3A%20There%20are%20language%20constructs%20that%20makes%20the%20code%20fault%20tolerant%20and%20robust%20like%20immutable%20by%20default%2C%20null%20value%20management%20and%20more.%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3ESupports%20multiple%20programming%20paradigms%3C%2FSTRONG%3E%3A%20F%23%20lets%20you%20choose%20the%20patterns%20and%20practices%20most%20effective%20for%20solving%20problems%20by%20providing%20strong%20support%20for%20functional%20and%20object%20programming%20paradigms.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH2%20id%3D%22toc-hId--1536025933%22%20id%3D%22toc-hId--1536025933%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId-951486900%22%20id%3D%22toc-hId-951486900%22%3E%3CA%20href%3D%22https%3A%2F%2Fdev.to%2Fazure%2Fwrite-your-first-code-in-f-36gl%23how-do-i-get-started%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20name%3D%22how-do-i-get-started%22%3E%3C%2FA%3EHow%20do%20I%20get%20started%3F%3C%2FH2%3E%0A%3CP%3EThere%20are%20a%20few%20things%20you%20need%20to%20get%20started.%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3E.NET%20SDK%3C%2FSTRONG%3E.%20You%20need%20to%20install%20the%20.NET%20SDK%20to%20be%20able%20to%20buiild%2C%20run%2C%20test%20and%20also%20create%20F%23%20projects.%20You%20can%20find%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdotnet.microsoft.com%2Fdownload%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EF%23%20SDK%20install%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ehere%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EIonide%3C%2FSTRONG%3E.%20It's%20a%20VS%20Code%20plugin.%20It's%20not%20necessary%20to%20install%2C%20but%20it%20will%20give%20you%20things%20like%20Intellisense%2C%20tooltips%2C%20codelens%2C%20error%20highlight%20and%20more.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH2%20id%3D%22toc-hId--855967563%22%20id%3D%22toc-hId--855967563%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId-1631545270%22%20id%3D%22toc-hId-1631545270%22%3E%3CA%20href%3D%22https%3A%2F%2Fdev.to%2Fazure%2Fwrite-your-first-code-in-f-36gl%23your-first-code%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20name%3D%22your-first-code%22%3E%3C%2FA%3EYour%20first%20code%3C%2FH2%3E%0A%3CP%3EYou%20can%20start%20writing%20F%23%20by%20using%20the%20REPL%20called%20FSI%2C%20you%20can%20also%20scaffold%20an%20F%23%20project%20using%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Edotnet%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eexecutable.%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--1972860552%22%20id%3D%22toc-hId--1972860552%22%3E%3CA%20href%3D%22https%3A%2F%2Fdev.to%2Fazure%2Fwrite-your-first-code-in-f-36gl%23create-a-project%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20name%3D%22create-a-project%22%3E%3C%2FA%3ECreate%20a%20project%3C%2FH3%3E%0A%3CP%3EYou%20create%20a%20project%20using%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Edotnet%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Elike%20so%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Edotnet%20new%20console%20--language%20F%23%20-o%20MyFSharpApp%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThen%20you%20get%20a%20project%20with%20files%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3EProgram.fs%3C%2FSTRONG%3E%2C%20your%20entrypoint%20file%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3EMyFSharpApp.csproj%3C%2FSTRONG%3E%2C%20your%20project%20file%20that%20contains%20everything%20it%20needs%20to%20know%20to%20build%20the%20project%20like%20dependencies%20for%20example.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH3%20id%3D%22toc-hId-514652281%22%20id%3D%22toc-hId-514652281%22%3E%3CA%20href%3D%22https%3A%2F%2Fdev.to%2Fazure%2Fwrite-your-first-code-in-f-36gl%23what-does-the-code-look-like%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20name%3D%22what-does-the-code-look-like%22%3E%3C%2FA%3EWhat%20does%20the%20code%20look%20like%3F%3C%2FH3%3E%0A%3CP%3EHere%20you%20have%20the%20code%20of%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CEM%3EProgram.fs%3C%2FEM%3E.%20Itt%20contains%20the%20functions%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Efrom()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Emain()%3C%2FCODE%3E.%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Emain()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eis%20the%20entry%20point%20of%20the%20app%2C%20as%20seen%20by%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3E%5B%3CENTRYPOINT%3E%5D%3C%2FENTRYPOINT%3E%3C%2FCODE%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-fsharp%22%3E%3CCODE%3Eopen%20System%0A%0A%2F%2F%20Define%20a%20function%20to%20construct%20a%20message%20to%20print%0Alet%20from%20whom%20%3D%0A%20%20%20%20sprintf%20%22from%20%25s%22%20whom%0A%0A%5B%3CENTRYPOINT%3E%5D%0Alet%20main%20argv%20%3D%0A%20%20%20%20let%20message%20%3D%20from%20%22F%23%22%20%2F%2F%20Call%20the%20function%0A%20%20%20%20printfn%20%22Hello%20world%20%25s%22%20message%0A%20%20%20%200%20%2F%2F%20return%20an%20integer%20exit%20code%3C%2FENTRYPOINT%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Efrom()%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Efunction%20takes%20the%20arg%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Ewhom%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%20the%20function%20body%20prints%20a%20string%20using%20the%20function%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%3Esprintf%3C%2FCODE%3E.%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-504149177%22%20id%3D%22toc-hId-504149177%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId-370464453%22%20id%3D%22toc-hId-370464453%22%3E%3CA%20href%3D%22https%3A%2F%2Fdev.to%2Fazure%2Fwrite-your-first-code-in-f-36gl%23how-do-i-learn-more%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%20name%3D%22how-do-i-learn-more%22%3E%3C%2FA%3EHow%20do%20I%20learn%20more%3F%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CSTRONG%3ELEARN%20module%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3EThe%20first%20F%23%20module%20of%20many%20was%20just%20published%20on%20LEARN%2C%20check%20it%20out%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-gb%2Flearn%2Fmodules%2Ffsharp-first-steps%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ETake%20your%20first%20steps%20with%20F%23%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CSTRONG%3ELEARN%20module%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3EThe%20second%20module%20just%20published.%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-gb%2Flearn%2Fmodules%2Ffsharp-first-program%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EWrite%20your%20first%20F%23%20programs%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Ffsharp%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EF%23%20docs%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2569795%22%20slang%3D%22en-US%22%3E%3CP%3EF%23%20is%20an%20open-source%2C%20cross-platform%20programming%20language%20that%20makes%20it%20easy%20to%20write%20succinct%2C%20performant%2C%20robust%2C%20and%20practical%20code.%20We%20just%20published%20a%20module%20on%20F%23%20on%20LEARN%2C%20many%20more%20to%20come.%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Co-Authors
Version history
Last update:
‎Jul 23 2021 03:36 AM
Updated by: