![]() ![]() Turn the console app into a Pig Latin generator Because if.then.else is itself an expression, evaluation of the body of the then block or the body of the else block determines the value returned by the toPigLatin 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. There's one final thing to notice about this: in F#, there's no explicit instruction to return from the function. Otherwise, move that first character to the end of the word and add "ay" to it. If it is, attach "yay" to the end of the word. In the body of the function, you'll notice two distinct parts:Īn inner function, called isVowel, that determines if a given character ( c) is a vowel by checking if it matches one of the provided patterns via Pattern Matching: let isVowel (c: char) =Īn if.then.else expression that checks if the first character is a vowel, and constructs a return value out of the input characters based on if the first character was a vowel or not: if isVowel word thenĬheck if the first character of the input word is a vowel. You'll also notice this if you hover over the function in Visual Studio Code. This is known as the type signature of the function, a fundamental piece of F# that's key to understanding F# code. This states that toPigLatin is a function that takes in a string as input (called word), and returns another string. You may have noticed the following in FSI: val toPigLatin: word: string -> string If it doesn't start with a vowel, move that first character to the end of the word and add "ay" to it. If the first character in a word starts with a vowel, add "yay" to the end of the word. If you're not sure about what the code is actually doing, here's a step-by-step.Īs you can see, toPigLatin is a function that takes a word as its input and converts it to a Pig-Latin representation of that word. The at the end lets FSI know when the code is finished. This is because FSI allows you to enter multiple lines. Congratulations, you just wrote your first F# function in Visual Studio Code and evaluated it with FSI!Īs you may have noticed, the lines in FSI are terminated with. The function appears to be working as expected. You should see the following result: val it: string = "appleyay" Now, let's try with a vowel as the first letter. You should see the following result: val it: string = "ananabay" The FSI process evaluated the code you sent over.īecause what you sent over was a function, you can now call that function with FSI! In the interactive window, type the following: toPigLatin "banana".It sent the code you highlighted over to the FSI process.You'll notice a terminal window pop up on the bottom of the screen, and it should look similar to this: Once it's highlighted, hold the Alt key and hit Enter. ![]() Highlight the entire function (it should be 11 lines long). The next step is to evaluate it using F# Interactive (FSI). ![]() This function converts a word to a form of Pig Latin. Now add the following code to it: let toPigLatin (word: string) = NET Core scripting, navigate to the Explorer view in Visual Studio Code and create a new file. Once you've configured Visual Studio Code to use. ![]() You can write code in the editor before this point in time, but once this happens, everything has finished loading. This means Ionide has successfully loaded the project you just created. Once it completes, change directory to the project and open Visual Studio Code: cd FirstIonideProjectĪfter the project loads in Visual Studio Code, you should see the F# Solution Explorer pane on the left-hand side of your window open. NET CLI: dotnet new console -lang "F#" -o FirstIonideProject To create a new F# project, open a command line and create a new project with the. To begin, ensure that you have F# and the Ionide plugin correctly installed. Visit Ionide.io to learn more about the plugin. You 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. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |