Saturday, March 21, 2020

Learning F# - Step 1

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:

  1. Create a Hello World program.
  2. Understand how projects are set up in the language.
  3. Learn unit testing in the language.
  4. Learn to work with the file system in the language.
  5. Learn to work with a database in the language.
  6. Learn to make web API calls in the language.
  7. Start learning the "particulars" of the language.
Using this approach allows me to start using the new programming language for real-life tasks, which helps my brain learn better.

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.fs
The 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 code
The 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 run
Lo 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#!

No comments:

Post a Comment