concept Fact function in category f#

appears as: Fact function, The Fact function
F# Deep Dives

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

In this listing you nest the Seq.reduce version of the Fact function into the Binomial function and then improve it a bit to cope with inputs less than 1.

Returning large integers from the Fact function

What happens if you alter the Fact function to return a large integer, as embodied in .NET’s BigInteger type? You need to tweak the tests a bit because big integers can’t be represented directly in [<TestCase()>] attributes. You’ll represent them instead as strings and parse them in the body of the test.

Listing 5. Expressing big integers as strings

You can also amend the Fact function to return a big integer.

Listing 6. Version of the Fact function that returns a big integer
let Fact n =
    match n with
    | _ when n <= 1 -> 1I
    | _ -> [1I..System.Numerics.BigInteger(n)]
           |> Seq.reduce (*)