F# Fundamentals

Introduction

A functional computer language, also called a functional programming language, also called a functional language, is a language that treats an action 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). To program in F#, you can use a programming language. For our lessons, we will use Microsoft Visual Studio and sometimes Visual Studio Code. Both are free. You can download them from one of Microsoft web sites (simply do an Internet search on Microsoft Visual Studio; you should immediately see a link to download one of these applications).

To follow every one of our lessons, you can perform the following actions.

Practical LearningPractical Learning: Starting a Project

  1. Start Microsoft Visual Studio
  2. In the Visual Studio 2022 dialog box, under Get Started, click Create a New Project

    Create a New Project

  3. In the Create a New Project dialog box, in the top combo box, select either All Languages or F#
  4. In the large list on the right side of the dialog box, and click Console App (it should be selected already)

    New Project

  5. Click Next
  6. Accept the suggested name or change it
    Accept or change the Location

    New Project

  7. Click Next
  8. In the Framework combo box, select the latest version (.NET 7.0 (Standard Term Support))

    New Project

  9. Click Create

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 indicate that printf is used to present a value, such as a word or a sentence to a user. To use it, follow one of these formulas:

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 formulas as printf. Therefore, you can use one of the following formulas:

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.

Accessories for Programming

Comments

A comment is section of code that is not processed in a program. The F# language 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, functions, and functionalities used to create a computer application. 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).

As an alternative, Microsoft created a new library named the .NET Framework. 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 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.

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.


Home Copyright © 2014-2024, FunctionX Monday 04 September 2016 Next