concept loop in category f#

This is an excerpt from Manning's book Get Programming with F#: A guide for .NET developers.
The biggest challenges you’ll face are first learning the syntax of F#, and then, more importantly, unlearning the dogma of OO methodology. Things such as for loops, classes and mutable state are so deeply ingrained into our mental model of domain modelling and problem solving that it can be difficult to forget them. F# does allow you to work with mutable data, imperative styles and classes – but doing these sorts of things in F# certainly isn’t idiomatic and the syntax will feel unnatural compared to C# or VB .NET – in essence, you’ll get the worst of both worlds. Instead, you’ll be better off starting from a clean slate when you approach solving problems, and you should rely on following a few simple rules and “behaviours” that we’ll cover throughout the book – if you can do that, your solutions will naturally end up in a functional style.
for and while loops
for-each and for loops can be modeled by using the for .. in construct in F#. for .. in loops also have a handy syntax for creating ranges of data quickly, so although there’s a separate construct in F# for simple for loops (known as for .. to), I’ve ignored it here.
Listing 20.1. for .. in loops in F#
for number in 1 .. 10 do 1 printfn "%d Hello!" number for number in 10 .. -1 .. 1 do 2 printfn "%d Hello!" number let customerIds = [ 45 .. 99 ] for customerId in customerIds do 3 printfn "%d bought something!" customerId for even in 2 .. 2 .. 10 do 4 printfn "%d is an even number!" even1 Upward-counting for loop 2 Downward-counting for loop

This is an excerpt from Manning's book F# Deep Dives.
In this section, you’ll take the first steps in implementing the formula for cumulative binomial distribution. In doing so, you’ll learn general lessons about coding calculations in F#, including how to perform iterative accumulation without loops, and how and when to use aliases and custom operators to simplify code.
The use of loops rather than recursion It’s probably worth reiterating at this point that there is nothing inherently wrong with the use of (local) mutable values or loops, but their presence is often an indicator that a more fundamental manifestation of the algorithm is available. Whether you listen to this cue is an interesting balance of performance, personal style, and readability. There’s no general right answer.
What still differs between the two while loops? Not much: they run for different ranges of k, a couple of flags and values are reset between the first and second loops, and two values set in the first loop are updated further in the second. Let’s write a function that’s equivalent to both loops and worry about how to invoke for both ranges later. The following listing shows a recursive function that embodies the logic from both manifestations of the loop.
Listing 14. Calculate function that replaces both loops
let rec Calculate k totalUnscaledProbability previous unscaled = let current = CalcCurrent previous k let totalUnscaledProbability' = totalUnscaledProbability + current let unscaled' = CalcUnscaled x k unscaled current if Done current then unscaled', totalUnscaledProbability' else Calculate (NextK k) totalUnscaledProbability' current unscaled'Listing 14 still uses the four functions you pulled out of the loops—these functions remain unchanged—and passes those values that need to change between iterations as parameters.

This is an excerpt from Manning's book Real-World Functional Programming with Examples in F# and C#.
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.
The C# compiler performs a rather sophisticated transformation on the iterator code to create a “hidden” type that implements the IEnumerable<T> interface. The interesting thing about listing 12.1 is how it works with the local state. We declare one local variable to store some mutable state ❶, and a second mutable variable is declared as part of the for loop. The algorithm is implemented inside a loop, which is executed every time we want to pull another value from the iterator. The loop body updates the local state of the iterator ❷ and yields the newly calculated value.
Listing 12.5. Generating an infinite sequence of random colors in C# and F#
// C# version using loops IEnumerable<Color> RandomColors(){ var rnd = new Random(); while(true) { ❶ int r = rnd.Next(256), g = rnd.Next(256), b = rnd.Next(256); yield return Color.FromArgb(r, g, b); ❷ } } // F# version using recursion let rnd = new Random() let rec randomColors = seq { ❸ let r, g, b = rnd.Next(256), rnd.Next(256), rnd.Next(256) yield Color.FromArgb(r, g, b) ❹ yield! randomColors } ❺
Now suppose we want to find the countries of the selected cities. We could iterate over the entered cities and find each city in the cities list to get the country. You can probably already see the problem with this approach: there is a city named Cambridge in both the United Kingdom and the United States, so we need to be able to return multiple records for a single city. You can see how to write this using two nested for loops in a sequence expression in listing 12.10.