F# – Hello World

I wanted to pick up a new programming language and learn as I go along, which will hopefully let you learn as well.  I have decided to learn F#.  I was going to do Python, and probably will but I found a great blog that has already done this using Python.  I would like to do a comparison between these languages as I know they are similar and F# was influenced by Python.  As of now, I still don’t have a good reason to learn F#, but it’s syntax looks similar to Lambda expressions in C#, so it hopefully will open my mind more to functional languages.  I took Scheme in college and found it to be a lot of fun, but not a very useful language, compared to a more object oriented based language.

The first thing I notice about F# is its succinctness.  It uses Type Inference, which means the compiler will figure out what type a variable is, or what a function will return.  So you don’t have to declare the types.

I can really see the functional programming style of Lambda (=>) expressions in C#.

Code Organization

A few basics of F#.  There are namespaces just like C#, but there are also modules.  Modules are the same of Namespaces except for a few exceptions, they allow storing of values to variables, and they cannot span across multiple files.  Also, a very interesting concept in F# that is different than C# is that the indent defines scope.

In C# your scope is defined with curly braces {}

 

But in F# the scope would appear as:

 

Creating HelloWorld function

You first need to mark the entry point with an attribute, the “EntryPoint” attribute simply enough.  Once you have your “main” function you need to declare the new function above that function so it knows that it exists.  Its as simple as:

 

As you can see there were no types declared and the compiler will know what type each variable and method is.  I will go over flow logic next week.

 

Leave a Reply

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