This simple example demonstrates how to use the "sum" function of the Seq module. In this example, four integers are added together. The result is then squared.
Note: I have chosen to write these sample tests in C# to help emphasis the interoperability of the languages. These tests could have just as easily been written in F#. For examples of this, check out the book Expert F#.
Test:
[TestMethod]
public void CanCalculateSeqExample()
{
List<int> numbersToCalculate = new List<int>();
numbersToCalculate.Add(1);
numbersToCalculate.Add(2);
numbersToCalculate.Add(3);
numbersToCalculate.Add(4);
int result = FSharpSample.CalculateSeqExample(numbersToCalculate);
Assert.AreEqual(result, 100);
}
.fs File:
#light
let CalculateNumberSquared x = x*x
let CalculateSeqExample args =
Seq.sum args |> CalculateNumberSquared
.fsi File:
#light
val CalculateNumberSquared: int -> int
val CalculateSeqExample: seq
Explanation:
While this example is very simple, it provides a brief view into the power that some of these modules provides. This example also shows how functions can be chained together. The astute observer will have noticed the "|>" operator in the