With COVID-19 forcing everyone inside and cancelling events, I suddenly find myself with a lot of extra time. For the first few days, I just enjoyed having my weeknights free. Now, I want to use that time to learn something new. There are several things that I may bite off, but I've decided to start by learning F#.
F# is the functional programming language created by Microsoft. I was surprised to find out that it is baked into the .NET SDK that you download to create .NET Core applications! That fact is what prompted me to learn F#.
To get started with F#, you need to download the latest .NET Core SDK. Once you have .NET Core installed, you can get started!
Before I dive in, I wanted to share a little bit about how I approach learning a new programming language. I like to learn technology by doing things. With programming, I like to write code that solves problems. Quite often they are problems that I've solved over and over again, but they are still problems. When I approach a programming language, I want to do the following things:
- Create a Hello World program.
- Understand how projects are set up in the language.
- Learn unit testing in the language.
- Learn to work with the file system in the language.
- Learn to work with a database in the language.
- Learn to make web API calls in the language.
- Start learning the "particulars" of the language.
Now that we know what language we are going to learn and how we are going to learn it, let's get started!
I have the latest .NET Core SDK installed on my box. To create the "Hello World" program, create a directory named FSharpHelloWorld. Change directory into FSharpHelloWorld, then all I have to do is:
dotnet new console --language f#This simple command-line call creates a basic F# program that prints "Hello World" to the screen. Unfortunately, Microsoft created the Hello World program for me. In the directory, you should see the following 3 files listed:
obj FSharpHelloWorld.fsproj Program.fsThe FSharpHelloWorld.fsproj file is the "project" file that tells the SDK what to build and how to build it. (I'll dive into this file more in my next post.) The obj directory holds build artifacts. The Program.fs file is the code file that will get compiled and run. Inside the Program.fs file, you should see:
// Learn more about F# at http://fsharp.org open System [<EntryPoint>] let main argv = printfn "Hello World from F#!" 0 // return an integer exit codeThe words "main" and "argv" look familiar to me as a C# developer, but the rest is a little foreign. I assume the "printfn" is something like a Console.WriteLine statement. The comments give me hints about the other line of the program. Since I have to feel like I've written code, I modify the text after "printfn" to read "Hola Mundo!" Next, I run the program by issuing the following command in my folder:
dotnet runLo and behold, I see
Hola Mundo!printed on the screen!
So, what did I really accomplish? First, I was able to set up the basic SDK and get a simple environment up and running. Second, I was able to generate a simple program and make it run. Third, I was able to modify the program and have it run without compilation or runtime errors. I would say I have accomplished a lot!
Future posts will track my progress learning F#!