GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing learn.microsoft.com — Get Started with F# in Visual Studio Code - .NET Note Access to this page requires authorization. You can try signing in or changing directories. Access to this page requires authorization. You can try changing directories. Get Started with F# in Visual Studio CodeYou can write F# in Visual Studio Code with the Ionide plugin to get a great cross-platform, lightweight Integrated Development Environment (IDE) experience with IntelliSense and code refactorings. Visit Ionide.io to learn more about the plugin. Note Before beginning, ensure you've installed both F# and the Ionide plugin. Explore F# with scripts and the REPLThe fastest way to start writing F# is with a script file ( Create and run a scriptOpen a terminal and run:
Add the following code to the file:
Run the script directly from the terminal:
You should see:
Use the interactive REPL from VS CodeScripts are especially powerful when combined with Ionide's FSI integration. Create a new file called MyFirstScript.fsx and add the following code:
This function converts a word to a form of Pig Latin. The next step is to evaluate it using F# Interactive (FSI). Highlight the entire function (it should be 11 lines long). Once it's highlighted, hold the Alt key and hit Enter. You'll notice a terminal window pop up on the bottom of the screen, and it should look similar to this:
This did three things:
Because what you sent over was a function, you can now call that function with FSI! In the interactive window, type the following:
You should see the following result:
Now, let's try with a vowel as the first letter. Enter the following:
You should see the following result:
The function appears to be working as expected. Congratulations, you just wrote your first F# function in Visual Studio Code and evaluated it with FSI! Note As you may have noticed, the lines in FSI are terminated with When to use scriptsScripts are great for exploring ideas, prototyping, data analysis, and automation tasks. You can reference NuGet packages directly in a script with When your code grows beyond a single file or needs to be compiled and distributed, you'll want to move to a full project. The next section shows how. Create your first project with IonideFor larger programs, compiled libraries, or code you want to distribute as an application, you'll use a .NET project. To create a new F# project, open a command line and create a new project with the .NET CLI:
Once it completes, change directory to the project and open Visual Studio Code:
After the project loads in Visual Studio Code, you should see the F# Solution Explorer pane on the left-hand side of your window open. This means Ionide has successfully loaded the project you just created. You can write code in the editor before this point in time, but once this happens, everything has finished loading. Explaining the codeIf you're not sure about what the code is actually doing, here's a step-by-step. As you can see, If the first character in a word starts with a vowel, add "yay" to the end of the word. If it doesn't start with a vowel, move that first character to the end of the word and add "ay" to it. You may have noticed the following in FSI:
This states that In the body of the function, you'll notice two distinct parts:
The flow of Check if the first character of the input word is a vowel. If it is, attach "yay" to the end of the word. Otherwise, move that first character to the end of the word and add "ay" to it. There's one final thing to notice about this: in F#, there's no explicit instruction to return from the function. This is because F# is expression-based, and the last expression evaluated in the body of a function determines the return value of that function. Because Turn the console app into a Pig Latin generatorThe previous sections in this article demonstrated a common first step in writing F# code: writing an initial function and executing it interactively with FSI. This is known as REPL-driven development, where REPL stands for "Read-Evaluate-Print Loop". It's a great way to experiment with functionality until you have something working. The next step in REPL-driven development is to move working code into an F# implementation file. It can then be compiled by the F# compiler into an assembly that can be executed. To begin, open the Program.fs file that you created earlier with the .NET CLI. You'll notice that some code is already in there. Next, create a new
This module should be above the Now, in the
Now you can run your console app from the command line:
And you'll see that it outputs the same result as your script file, but this time as a running program! Troubleshooting IonideHere are a few ways you can troubleshoot certain problems that you might run into:
Ionide is an open-source project built and maintained by members of the F# community. Report issues and feel free to contribute at the ionide-vscode-fsharp GitHub repository. You can also ask for further help from the Ionide developers and F# community in the Ionide Gitter channel. Next stepsTo learn more about F# and the features of the language, check out Tour of F#.
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
FeedbackWas this page helpful? NoAdditional resources
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |