Guys,

In this post i am going to talk about about the F# application structure.

  • If you have worked with any programming languge then i would expect you to know that modules are the building blocks of an application which form the structure of an application. These are the container that contain the F# functions, types and values. We could also think of a module as a static clas in C#
  • If F# we could have multiple modules in the same code file but one module could not span multiple code files.
  • A module lives in the .Net namespace hierachy. This could be controlled by the programmer as to where does the module resides in the .Net namespace hierarchy. If we are writing a simple F# program we might not see modules being cretaed as that is taken care by the F# comipler internally. Also as you would expect from F# having really good inference capbility, it automatically recognizes the entry function (main function) and structures it.
  • If we have multiple possible entry points in the F# application then we need to use the [<EntryPoint>] attribute to define the entry point of the program manually do that the F# compiler recognozes where to start.
  • Let see this action. Add the following code to an F# application and run it. We ould see something like the below screen:

 

Run F# Function

Run F# Function

  •  Now lets add a new module in this application. Right click on the project name and select add new item and add a new F# sourcefile and name it Calcy.fs
Add New Item F#

Add New Item F#

 

Add New F# Source File

Add New F# Source File

  •  Once you hit ok, you would see that the new file is cretaed and the it starts with Module Calcy. Lets do one thing lets cut the first 2 lines of code from our program.fs and paste it below module Calcy in Calcy.fs
F# Module

F# Module

  •  Now in Program.fs we we would see that the compiler is complaining. So what we need to do is prefix the intAdd function with our module name like below:
Call Function from another module

Call Function from another module

  •  But you can see that the error still persists. Wonder why? Its because F# evaluates functions as well as files in the order they appear. So as the Calcy.fs is present below the program.fs in solution explorer we cannot use the functions present in Calcy module. So just right click the Calcy.fs file and click on move up and you see the error disappear from your Program.fs file. We could run the application and see that it works absolutly fine.
Move File Up F#

Move File Up F#

  • The F# compiler would still automatically create a module from the program.fs file but if want you refer it from a from program in other .Net language then we should know the name of the module and hence it is a good idea to give the module in Prorogram.fs a name. So change the code in Program.fs as below:
F# EntryPoint

F# EntryPoint

  •  Now here we have multiple things to note. We have specified the module name. We have specified that this main function is the entry point to our application. Also you would see that i have specified a return value of 0 for the main function. This return value is required as the main method expects a return value of type int.
  • Now lets modify the Calcy.fs and make it contain 2 modules. You would still see that there is an error which expects us to specify a namspace so that distintion can be made between the modules.
Multiple Modules in 1 File F#

Multiple Modules in 1 File F#

  •  Lets add the namspace as global and change the code accordingly in Program.fs as the module names have changed and run the app and it should work fine.
namespace global F#

namespace global F#

 

Run F# App

Run F# App

  •  Now if we want to use any other custom namespace other than the global namespace then we need to open that namespace before we can use it otherwise the F# complier will complain. In F# we could also just opn the module instead of the whole namespace and this is an extention the using keyword in the C# language. This gives use the flexibility to remove the module name suffix before the intAdd funstion in Program.fs
Custom Namespace F#

Custom Namespace F#

  •  If we modify Program.fs module to be part of the same namespace as Calcy.fs modules we do not need to open the namespace which makes perfect sence.

You can find the source code for this post here

 

Any questions, comments an feedback are welcome.

 

 

Guys,

In this one i am gonna talk about how we can create and call functions in F#.

  • In F# functions are also values which is evident because they use the same syntax with the let keyword as assignments.
  • When you are writing functions that span multiple lines you need to use indentation to define the function structure.
  • Functions are nestable in F#.
  • In F# we don’t need to expliclitly define the return value of the function. The last expression which is evaluated in the function would become the function’s return value automatically.
  • As you have already seen in the previous posts that the inference engine of F# is pretty great and as you would expect input parameters and return values are inferred automatically by the compiler and hence the signature of the function is when we are calling a function in F# automatically defined.
  • When we are calling a function in F# we don’t need to put any delimiters. We need to use the parentheses for specifying priority only for we are making nested calls.
  • Lets declare a function and follow it with space and followed by the parameter followed by the evaluation and followed by the return value.
int Square

int Square

  •  Now lets create a function with two input parameters. Now this function will be a multi line function. keep in mind when you are writing a multi line function then indentation is very important and if you do not follow the indentation then you might get the following warning.
F# Function Indentation

F# Function Indentation

  •  Now lets talk about the add function. As you could see in the image below when i take my mouse over to the intAdd function then we see that intAdd is a value if type int which goes to int goes to int.  Let me explain what i mean by that. Add is a value of type int as all values in F# have a type. int goes to int goes to int means there are are two int parameters coming in and one int value coming out. It might seem odd to you that the two int inputs are separated by the go to operator but thats how it is in F# as all the functions are in carried format. We would talk more about that later.
int Add Function F#

int Add Function F#

  •  As you know that functions are just values we could nest them according to our requirement. Lets create a new function intAddNested. Here intAddNested is the function and we have nested a value inside it with result as the return value.
Nested Function F#

Nested Function F#

  •  Now lets call the function that we just created. We dont need to use paranthesis in the same way as we don’t need to use the paranthesis while declaring the function. To call the function just type the following code in your F# program. Here add5and3 is the name of the type and the call to the function is followed by the =.
Function Call F#

Function Call F#

  •  Now lets see how we can use the return value from one function in the call to the other function. Here we could see we have used the return value of the intSquare function in intAdd function. As in standard maths the part of the function call inside the paranthesis is called first and then the other part o fthe function is executed.
Multi Function Call F#

Multi Function Call F#

  •  You can select all the code and send it to interactive to evaluate your code.

You can find the source code of this post here

Any questions, comments and feedback are most welcome.

 

 

Hey Guys,

In this post i am going to talk about Values, Data Types and Types Inferences in F#.

 

Values in F# are what Variables are in some other languages. They are declared and assigned using the keyword “let”. By default simple values are immutable and thats the reason why they are not called variables. Their types are automatically inffered automatically by assignment. But this does not mean that F# is a dynamic language. It is still a statically typed language but its just that the type inference works so well that you would rarely find any explicitly assigned types. For litrels F# has a complete list of the suffixes available. Some of them are u for uint, y for byte, L for int64, l for bigint, m for decimal and so on.

If you would have worked with any of the programming languages you would know that a variable is used to hold a value. F# has variable as well but it also has values which are not similar to variables so they cannot be chaged at runtime.

You can declare a variable in F# by sinpmly writing

let  myVariable = 100

and now when you hover over the mouse on the variable you would see that F# has inferred it as int. But we cannot chage the value of myVariable and when we try to assign a value to the  myVariable then the compile would give a warning that it would expect the value comparision instead of a new assignment to value of  myVariable.

Immuteable Variable

Immuteable Variable

The compiler is catually comparing if myVariable has value 111 and its not reassigning it. Also if you try to reassign the value of myVariable saying

let myVariable = 111 then we will get an error that the definition of myVariable is being duplicated.

But if you try to do the same thing in the interactive enviorment (for details of how to use the interactive enviorment visit F# Interactive enviorment/) it would work fine as the interactive enviorment is designed to work for the evaluation and revaluation. But this will not work in the application context.

Now let type the different types of values for different variables and mouse over over them and see how they are inferred.

Data Types in F#

Data Types in F#

You could take your mouse over each of the variables and could see the way it is inferred and type other types as well and see if they are correctly inferred or not.

So we can see that the standard value types in F# are not muteable..

Any Comments, Questions and Feedback are most welcome…

 

In this post we would talk about REPL which is the provided by the F# interactive enviorment. It could be used as tool window inside Visual studio or as stand alone command line application. Its really useful  as we can evaluate the Code within the Visual Studio enviorment easily. It can used during or before editing the code in the editor. Lets see how this works.

  • Create a new F# application and add the following code to it.
F# App

F# App

  • The use of the interactive enviorment is pretty easy. Just right click on the first line of code and select – Send to Interactive.
F# Send To Interactive

F# Send To Interactive

  •  It will automatically bring up the interactive window within the F# enviorment and evalute the selected line of code.
F# interactive

F# interactive

  • We can also type in the interactive window for evaluatig the code, but once we type the code to evaluate and hit enter the evaluation will not start so we need to terminate the code line with two semi colons and press enter. As F# does not have its own line termination but in the intercation window we need to treminate the statement with two semi colons. Once you press enter you would see the statement is evaluated and the interactive tool tells you the variable delared is an integer with value as 100.
F# Interaction Line Ternination

F# Interaction Line Ternination

  •  Now you can evaluate almost any F# code using the Interactive evaluation there and some restriction on the automatic evaluation. Now if you evalute a System.Console.ReadLine, it would work perfectly fine but if you select all the code written above in the Program.fs and send it to interactive the Console.ReadLine would retrieve an empty string. So we need to to careful at some places withis tool.
  • The compile langauages before F# did not had this capability so its really nice to have this stuff but you need to be careful while using it.

Any feedback, comments and questions are requeted.

 

Hope you guys had a look at my other F# posts

In this post i am going to talk about how we can create a Hello World program in WPF using F#.

We would need to manually create and include XAML files in WPF projects, they could also be loaded from code at runtime. We would also need to write the code to hook up event handlers. Let’s get started.

  • Create a new F# application named HelloWorldWPF

 

Create WPF F# Project

Create WPF F# Project

  • There is no way add a xaml file to a console application so either you would need to create an xml file and start editing it or use any other tool (Like Expression Blend) to create a xaml file and then include it in the project. i have used the 2nd option
F# Add XAML File

F# Add XAML File

  •  You can have a look at the xaml file below:
F# XAML File

F# XAML File

  • Lets add the references required by the WPF application. Add a reference to PresentationCore, PresentationFramework, System.Data, System.Deployment
  • Open namespace System, System.Windows and System.Windows.Controls. The let keyword is used for simple values.
  • Create a function that will load my main windows. So lets create a function named loadMyWindow().
  • Then define a new Uri for loading our MainWindow.xaml. This is the standard .Net specification. We don’t need any line terminatio as F# terminaties lines automatically.
  • Then we create a window using the resource loaded in the resourceLocator and then we need to cast this to Window using the F# cast operator.
  • The we find Element which is our clickButton and and we add a function to the Click Event of the Button. In F# we need to also handle all the return vales and hence we have used F# helper function ignore to ignore the values. In F# the last keyword in the function is the return type of the fuction which in our case is a window.
  • Now we would pass this window to the Application.Run and we ignore the retun value as well. We also then apply the STAThread attribute.
WPF HelloWorld

WPF HelloWorld

  •  Now we run the application and this is what we see.
WPF HelloWorld

WPF HelloWorld

 

Any Feedback, comments or questions were most welcome.