concept number in category f#

appears as: numbers, number, number, numbers, The number, A number
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.

Listing 0.2  Imperative and Declarative code samples in C# and F#
IEnumerable<int> GetEvenNumbers(IEnumerable<int> numbers) { var output = new List<int>(); #1 foreach (var number in numbers) { #2 if (number % 2 == 0) #3 output.Add(number); } #4 return output;} let getEvenNumbers = Seq.filter(fun number -> number % 2 = 0) #5 #1 : Temporary collection to store output #2 : Manual iteration through collection #3 : Actual filter logic #4 : Manual addition to output collection #5 Focus on business logic.

In the declarative version, we’re more interested in our intent than the implementation details. We don’t need to know the details of how theSeq.filter function is actually implemented because we understand the general logic of how it behaves. Armed with this, we can simply focus on our core goal, which is to identify numbers that are even. The code is much smaller – there’s less to read, less to think about, and less that can go wrong. Of course, you can achieve similar code in C# via LINQ; in fact, if you typically use LINQ for these sorts of operations today, that’s great! In F#, this is the default way of working - so you’ll find that this style of code will be much more common throughout your applications.

Spend a little more time in the F# script; write a function, countWords, that can return the number of words in a string by using standard .NET string split and array functionality. You’ll need to provide a type hint for the input argument, such as

let countWords (text:string) =...
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":15},{\"line\":0,\"ch\":16}],[{\"line\":0,\"ch\":20},{\"line\":0,\"ch\":28}]]"}
!@%STYLE%@!

Then, save the string and number of words to disk as a plain-text file.

Figure 16.5. pairwise operation on a list of numbers
F# Deep Dives

This is an excerpt from Manning's book F# Deep Dives.

In this chapter, we’ll look at the business motivations behind using F#, drawing from a number of case studies made by existing F# users. The technical aspects of many of the case studies are explained in later chapters by the people who developed and successfully deployed them. We’ll start with a business situation that many F# users share, and then we’ll look at the business problems they faced and how they solved them.

Figure 5. In Twitter, the in-degree is the number of followers and the outdegree is the number of friends (number of followed accounts).

Using the ==> operator works nicely when the number of invalid inputs isn’t too high. When you need to build more specific inputs, you can also specify custom data generators that randomly generate inputs that satisfy all your requirements.

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.

3.4.1. Processing lists of numbers

Imagine that we want to write a method similar to SumList but that multiplies the numbers rather than adding them. Making this change looks quite easy: we can copy the SumList method and tinker with it. There are only two changes in the modified method:

int MultiplyList(FuncList<int> numbers) {
if (numbers.IsEmpty) return 1;                        ❶
else return numbers.Head * MultiplyList(numbers.Tail);    ❷
}

The function is a special kind of type in F#. Similarly to tuples, the type of a function is constructed from other basic types. With a tuple, the type was specified in code using an asterisk between the types of the elements (e.g., int * string). In the case of functions, the type is specified in terms of the types of arguments and the return type. This provides type safety in the same way delegates do in C#. A function that takes a number and adds 1 to it would be of type int -> int, meaning that it takes an integer and returns an integer. The type of a function that takes two numbers and returns a number would be of type int -> int -> int, and this is exactly the type of the first parameter in our aggregateList function. Listing 3.18 shows the F# version of our example.

Figure 10.1. Stack frames when summing numbers in a list. In the first case, the stack frames fit within the limit, so the operation succeeds. In the second case, the calculation reaches the limit and an exception is thrown.