Home

Introduction to F#

 

F# Fundamentals

 

Introduction

A functional computer language, also called a functional programming language, also called a functional language, is a language that treats an assignment as an object. That is, a function is something that has a name, holds a value, and can perform actions.

To support functional languages and functional programming, Microsoft created a language named F# (pronounced F sharp).

Introduction to Functions

A function is the central point of a functional language. It can perform an action. It defines a purpose. And it can produce a value.

Introduction to Built-In Functions

To assist you with programming, the F# language provides many already-created functions, referred to as built-in functions. One of the built-in functions of F# is named printf.

Although we will see various examples of using that function, we can first indique that printf is used to present a value, such as a word or a sentence to a user. To use it, follow this formula:

printf Something to show
printf (Something to show)
printf Something to show;
printf (Something to show);

After printf, you can include something in double-quotes or include the double-quoted value inside of parentheses. You can end the whole statement with or without a semi-colon. Here is an example:

printf ("Welcome to the wonderful world of functional programming!");

When printf has performed its job, any statement that must be executed after it would follow after it and on the same line. If you want the execution to continue on the next line, use a variant of this function. It is named printfn. This function follows the same formula as printf:

printfn Something to show
printfn (Something to show)
printfn Something to show;
printfn (Something to show);

There are many other rules we will see about using either printf or printfn.

 
   
 

Comments

A comment is section of code that is not processed in a program. C# supports two types of comment. To create a comment on one line, start the line with //. Anything on the right side of // will be ignored. This means that // supports comments on only one line.

To create a commented section that covers one or more lines, start the section with (* and end it with *). Anything inside will be ignored in the program.

The .NET Framework Library

A computer library is a set of objects (classes), functions, and functionalities used to create computer applications. There are many libraries from various companies.

At one time, Microsoft created the Microsoft Windows (3.0 to 95) operating system. To allow people to create applications that can run on those operating systems, Microsoft created a library named Win32. That library has always been available to create applications for all Microsoft Windows versions. The Win32 library was primarily created for C programmers. In reality, to program in Win32, you need a good understanding of C and/or preferrably C++. Of course, there are other languages you can use to create applications for Microsoft Windows. Examples of languages are Assembly (Assembly is not a true language per se; it is a set o languages and various languages have different flavors of Assembly) or Object Pascal (Embarcadero Delphi).

C++ is not an easy language with its arrays and pointers, etc. As an alternative, in the late nineties and early 2000s, Microsoft created a new library named the .NET Framework.

Ever since the release of the .NET Framework, all languages that Microsoft developed after that period rely on that library. These languages include C#, Visual Basic, and now F#. This means that part of the features you will use in F# depend on the .NET Framework.

In our lessons, when a feature we need to use is mostly part of the .NET Framework, we will point it out; but as much as possible, when a feature is available in both the F# language and the .NET Framework, we will prefer F#.

F# Libraries

To implement the F# language, Microsoft created a set of libraries. Those libraries were added in the .NET Framework. This means that you need the .NET Framework installed on your computer in order to fully use F#. If you install Microsoft Visual Studio, it would take care of the .NET Framework.If you install a version that includes F# such as Microsoft Visual Studio Professional or better, it would take care of F#.

Attributes

One of the features of the .NET Framework is called an attribute. An attribute is a technique used to extend the functionality of something. You will hardly need to create an attribute (in fact in our lessons, we will not create them). Most or all of the attributes you will need have already been created and are part of the .NET Framework. You can simply used them when necessary.

An attribute appears as follows:

[<Attribute-Name>]

When necessary, we will indicate when and where to (and why) use an attribute.

   
   
 

Previous Copyright © 2014-2015, FunctionX Next