Thursday, February 12, 2009

How I'm Learning F# - Writing the First Application

Over the past few months, I've been hearing more and more about the use of functional programming concepts and also languages like Haskell and F#.  While some of the initial musings that I've read revolved around how the concepts have been around for decades and that it makes financial and scientific applications easier to read and write, I couldn't find a good reason to start learning it for my typical line-of-business application design and development job or even some of my basic hobby projects.  Nonetheless, I kept getting drawn to the concept and have decided to focus on learning it.

This post marks the second entry of a series that I'll be writing to discuss how I'm going about learning F#.  While I'm not saying that my method of learning is ideal and should be followed by others, I'm just reporting how I'm going about doing it.  Over the course of this series, my goal is to provide other .Net developers a(nother) resource for learning the F# language as well as apply the language into some non-financial or scientific scenarios.

 

Series Table of Contents:

  1. Finding Resources
  2. Writing the First Application

 

Downloadable Resources:

 

Setting Up Visual Studio 2008 with F#:

As of this writing, F# is at CTP status since September 2008 (v1.9.6.2).  For this example, and possibly for the remainder of the series, I'll be using this September 2008 CTP of Microsoft F#.  This CTP installer only works with Visual Studio 2008.  The CTP version does not work with Visual Studio 2005 (which previous versions did) nor the Visual Studio Express editions.  If you would like to use F# using Visual Studio 2005, please refer to this StackOverflow question about how to set up F# in Visual Studio 2005 using an older version. NOTE: If you are using an older version of F# for use in Visual Studio 2005, I cannot guarantee that the examples presented in this series will work.  After you have downloaded and installed F# (either the CTP or a previous version), we are ready to begin writing our first F# application.

 

Writing the "Hello,World!":

Like many great computer books, this series will be starting with a simple example which we'll dissect afterwards.  In order to write the example, we'll need to start a new F# project in Visual Studio.  If we go to File > New Project..., we are treated to the following dialog window:

NewProjectDialog

The projects listed under Visual F# include a F# Application, a F# Library, and a F# Tutorial.  For the sake of this example, we'll be creating an F# Application named HelloWorldFSharp.  After clicking on the OK button, Visual Studio creates the new F# project containing a single Program.fs file. 

When the project is created, the Program.fs file contains a single line #light.  This tells the F# compiler to use the lightweight syntax of F# which removes some of the verbosity of the language and applies rules around white-space.  In the majority of examples on the Internet and books, the lightweight syntax is used and will be used going forward here as well.

Under the #light line, add the following code snippet:

let question = "How are you?"

let sayHello name =
    "Hello, " + name + "! " + question

In this code, we are doing two things.  First, we are declaring a variable named question.  Second, we are declaring a method called sayHello and takes a parameter called name. The sayHello method concatenates some strings together and returns the composed string. When using the #light syntax, every indented line is considered part of the function or action with the last line being the return value.

One thing to mention about F# when compared to other languages is that variables are immutable.  Once a variable is declared (and given a value), that value can never change.  I believe there are some exceptions to this when interacting with object properties; however, at the core of the language, basic variables cannot be re-declared nor their values redefined after they have been initially declared. Because of this, many F# documents and tutorials will use "values" instead of "variables" to prevent the confusion.  I'll attempt to follow this pattern.

 

One thing that a developer coming from VB.net or C# may notice is that there are no types defined here.  F# infers typing based on the value and how they are used.  If we wanted to declare a type for our question and name values, the code would like like the following:

let (question : string) = "How are you?"

let sayHello (name : string) =
    "Hello, " + name + "! " + question

In F#, you can declare the type by using (variablename : type) syntax.

 

Now that we've seen how to declare a value and a method, we can output a value to a console window using the following line:

printfn "%s " (sayHello "James")

Here, we are using the printfn method which prints formatted text to the console window.  The %s is a placeholder for a string value which we are supplying by calling our sayHello method passing in the name "James". In this example, we're treating the method like a value.

 

Here is the full code of Program.fs.

   1: #light
   2:  
   3: let question = "How are you?"
   4:  
   5: let sayHello name = 
   6:     "Hello, " + name + "! " + question
   7:  
   8: printfn "%s " (sayHello "James")

 

Running the Program using F# Interactive:

At this point, we can either go the Build menu and build the project like many other console applications or we can use a feature unique to F# called the F# Interactive window.  F# Interactive (fsi.exe) allows you to run sections of your F# program as well as write ad-hoc F# statements.  To use F# Interactive from within Visual Studio, highlight the code you wish to run and press Alt+Enter.  A new Visual Studio panel opens for F# Interactive and reports information about the code (or the error it generates).

For example, if you were to highlight the line where we declare our value question and press Alt+Enter, the following will appear:

val question : string

What this line explains is that we have a value called question that is of type string. If we do the same to our sayHello method, the following appears:

val sayHello : string -> string

This line states that sayHello is a value that accepts a parameter of type string and returns (signified by the -> ) a value of type string.  If we highlight the all of the code we've written so far, we are shown the following:

val question : string
val sayHello : string -> string

Hello, James! How are you?

This shows us the same output as we had previously for question and sayHello.  In addition, it executes the last line and displays the output.

 

Conclusion:

While this wasn't the largest demo nor the most unique, it is a simple step forward.  The premises of immutable variables (values) being declared and understanding the lightweight syntax are important to learning F#.  Over the next post of the series I'll be making some different applications in order to continue to explore F#'s syntax and it's power.  F# can do pretty much everything that VB.net or C# can do (with a few exceptions I'm now finding out) and hopefully the next posts will help us begin to find examples of where to apply it.


kick it on DotNetKicks.comShout it

Wednesday, February 4, 2009

How I'm Learning F# - Finding Resources

Over the past few months, I've been hearing more and more about the use of functional programming concepts and also languages like Haskell and F#.  While some of the initial musings that I've read revolved around how the concepts have been around for decades and that it makes financial and scientific applications easier to read and write, I couldn't find a good reason to start learning it for my typical line-of-business application design and development job or even some of my basic hobby projects.  Nonetheless, I kept getting drawn to the concept and have decided to focus on learning it.

This post marks the beginning of a new series that I'll be writing to discuss how I'm going about learning F#.  While I'm not saying that my method of learning is ideal and should be followed by others, I'm just reporting how I'm going about doing it.  Over the course of this series, my goal is to provide other .Net developers a(nother) resource for learning the F# language as well as apply the language into some non-financial or scientific scenarios.

 

Series Table of Contents:

  1. Finding Resources

 

Where To Start:

One of the challenges of learning anything is to find where to start gathering the necessary information on the subject matter.  Thankfully, there's already a great number of sources that can be used to learn F#.  Here are some that I've been using to get started in F#:

Of these 4 resources, I've so far leaned on StackOverflow and Expert F# the most.  I have examined only a little bit of the MSDN site and have been using it if I cannot find my answer on StackOverflow.  I initially began reading Foundations of F#; however, I was having a hard time building on each chapter.  Expert F# flows a lot better and also build on each previous chapter which makes it ideal for learning.

 

Why am I learning F#:

I'm learning F# in order to broaden my skill set into functional programming.  Initially, there wasn't any other reason for that.  I looked at OCaml, Haskell, and F# as all possibilities and eventually settled on F#.  Since then, I have began seeking a better reason for learning F#.  Many of reasons presented in interviews and blogs and books talked about it's use in financial and scientific fields of study.  While this is fine, I was still looking for more since I, like many other developers, am not in those fields of application development often.  After diving deeper, I found that F# is a very concise language that allows developers to write some code faster and in a more logical way.  Where as there is a large amount of code used to write a program in OO for the sake of being OO, F# allows a very simple and straight forward syntax to write programs or pieces of .Net applications in a functional manner.

 

What F# can do:

One other thing that I was fooling myself with once I began learning F# was this idea that it could or couldn't do something that a different .Net language could.  It wasn't until I began reading Expert F# that I truly understood how narrow minded I was being.  In Expert F#, the authors do a fantastic job at illustrating not only the language in a general sense, but also how to apply F# as a replacement to VB.net or C# in applications like ASP.Net, Winforms, and even Domain Specific Languages.  While, from what I can tell, many people agree that replacing C# or VB.net with F# in the ASP.Net realm isn't the best or most recommended application for the language, it does illustrate the point that F# can do anything that C# or VB.Net can do today.

 

Where does F# fit:

I have a feeling that I may be skewered a bit for this; however, this is just my perception as a novice F# developer at this point.  I've been developing in Microsoft technologies for years now and have also done work in other languages.  Where I see F# fits is where you have functional steps of processing of data.  I can see F# being a great language for writing the data engines for custom ETL applications and processes, as well as similar business logic processors.  I am certain I'll find additional locations that would assist me in my daily development job as I learn more of F#.

 

What's Next:

Next up in the series we'll dive into setting up VS2008 with the F# CTP and write the ever so popular "Hello, World!" or something like that :-).  Until then, I highly recommend checking out the F# resources mentioned above and also read a lot of the questions currently out there on Stack Overflow.


kick it on DotNetKicks.comShout it