concept program in category f#

appears as: program, programs, programs, The program
Get Programming with F#: A guide for .NET developers

This is an excerpt from Manning's book Get Programming with F#: A guide for .NET developers.

I’m sure you’ve heard that global variables are a bad thing! Scoping of values is important in any language; scoping allows us not only to show intent by explaining where and when a value is of use within a program, but also to protect us from bugs by reducing the possibilities for a value to be used within an application. In C#, we use { } to explicitly mark scope, as shown in the next listing.

Listing 4.3. Scoping in C#
using System public static int DoStuffWithTwoNumbers(int first, int second) { var added = first + second; Console.WriteLine("{0} + {1} = {2}", first, second, added); var doubled = added * 2; return doubled; }

In this context, the variable added is only in scope within the context of the curly braces. Outside of that, it’s out of scope and not accessible by the rest of the program. On the other hand, F# is a whitespace-significant language: rather than using curly braces, you have to indent code to tell the compiler that you’re in a nested scope.

Real-World Functional Programming with Examples in F# and C#

This is an excerpt from Manning's book Real-World Functional Programming with Examples in F# and C#.

Once the program grows, the number of commands in our vocabulary becomes too high, making the vocabulary difficult to use. This is where object-oriented programming makes our life easier, because it allows us to organize our commands in a better way. We can associate all commands that involve a customer with some customer entity (a class), which clarifies the description. The program is still a sequence of commands specifying how it should proceed.

Functional programming provides a completely different way of extending the vocabulary. We’re not limited to adding new primitive commands; we can also add new control structures—primitives that specify how we can put commands together to create a program. In imperative languages, we were able to compose commands in a sequence or by using a limited number of built-in constructs such as loops, but if you look at typical programs, you’ll still see many recurring structures—common ways of combining commands. In fact, some of these recurring structures are very well known and are described by design patterns. But in imperative languages, we keep typing the same structure of code over and over again.

1.3.3. Understanding what a program does

In the usual imperative style, the program consists of objects that have internal state that can be changed either directly or by calling some method of the object. This means that when we call a method, it can be hard to tell what state is affected by the operation. For example, in the C# snippet in listing 1.1 we create an ellipse, get its bounding box, and call a method on the returned rectangle. Finally, we return the ellipse to whatever has called it.

In functional programming, most of the data structures are immutable, which means that we can’t modify them. Once the Ellipse or Rectangle is created, we can’t change it. The only thing we can do is create a new Ellipse with a new bounding box. This makes it easy to understand what a program does. As listing 1.2 shows, we could rewrite the previous snippet if Ellipse and Rectangle were immutable. As you’ll see, understanding the program’s behavior becomes much easier.

Listing 1.2. Working with immutable ellipse and rectangle (C#)
Ellipse ellipse = new Ellipse(new Rectangle(0, 0, 100, 100)); Rectangle boundingBox = ellipse.BoundingBox; Rectangle smallerBox = boundingBox.Inflate(10, 10); ❶ return new Ellipse(smallerBox); ❷

When you’re writing programs using immutable types, the only thing a method can do is return a result—it can’t modify the state of any objects. You can see that Inflate returns a new rectangle as a result ❶ and that we construct a new ellipse to return an ellipse with a modified bounding box ❷. This approach may feel a bit unfamiliar the first time, but keep in mind that this isn’t a new idea to .NET developers. String is probably the best-known immutable type in the .NET world, but there are many examples, such as DateTime and other value types.

Functional programming takes this idea further, which makes it a lot easier to see what a program does, because the result of a method specifies fully what the method does. We’ll talk about immutability in more detail later, but first let’s look at one area where it’s extremely useful: implementing multithreaded applications.