Introduction to F# : Hello World!

Every time I begin to learn a new language I like to start with the classic “Hello World!”.  To me it just feels right and it’s nice to get that rapid gratification that comes along with going from nothing to something working in a short time.  Over the years I’ve found that new endeavors, personal or professional, can be hampered or halted by lack of realized results despite the best intentions.

Starting off, using Visual Studio 2015, I selected the Console Application F# template from the New Project dialog.  Using this template creates essentially three files in our project which look pretty familiar except for the .fs extension.

F# console application default solution

Program.fs seems like a good place to start. Let’s take a look at what the default contents of that file look like:

F# console application default Program.fs

We can infer a few things about what we see here

  • No curly braces, namespace, or class definition.  What did I get myself into?
  • “main” seems to be assigned an attribute that indicates it is the entry point for the application
  • Assuming argv contains arguments (hovering over it shows that it is indeed a string array)
    • This also hints to me that function parameters aren’t necessarily wrapped in parenthesis
  • printfn seems to be a print function and since this is a console app I’m guessing it’s roughly equivalent to Console.WriteLine()
  • And the last line 0 has a nice little comment that explains its purpose

Simply running this application and a quick bing pretty much confirms my rough assumptions.  Although, printfn seems a bit more interesting than Console.WriteLine() and definitely will be worth more reading.

Note: the let keyword as described on MSDN says “A binding associates an identifier with a value or function. You use the let keyword to bind a name to a value or function.”

It doesn’t look like much will be required to turn this default code into what we’re looking for.  However, if you ran the first default code via F5 – Start Debugging you would have seen a console window appear and fairly rapidly disappear.  You’ll see below that I’ve solved this with a Console.ReadLine() but there’s more to it, let’s explore:

F# console application Hello World!

Firstly, you should see open System which is new at the top; this allows us to use components in the System namespace which is where the command we were looking for lives.  Next Console.ReadLine() was inserted below our modified printfn command.

Simple, but wait… what is this |> ignore ??

In F# every expression must evaluate to a value, and all values must go somewhere.  Since our Console.ReadLine() function results in a string value we need to do something with it.  Conveniently enough there is a function ignore that will do just that for us.

Console.ReadLine() issue

ignore has a signature which takes one input and returns a type of unitunit seems to be equivalent to voidprintfn also returns unit which is why we didn’t have to pass the result to ignore.

Lastly the pipe-forward symbols |> essentially passes the result of the first expression to the last parameter input of the next (see next image for a rough visualization)

Pipe forward visual

Well that was more than I expected to get out of this “Hello World!” example, I hope this helps someone start their own journey into this interesting language.  At the time of this writing I’ve already coded a FizzBuzz implementation (actually multiple solutions) which I will post soon as a follow up in this introduction to F# series.

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>