Built-in factorial function in Haskell

No, but you can easily write one. If you are concerned about having to rewrite the function each time you need it, you could always write it as part of a module or a library (depending on how far you want to take this, any how many other similar functions you have). That way you only need to write it once, and can quickly pull it in to any other projects when you need it.


Try Hayoo! to search (link on the top of hackage); it came up with this, for example

http://hackage.haskell.org/packages/archive/statistics/latest/doc/html/Statistics-Math.html#v:factorial


Even though it is commonly used for examples, the factorial function isn't all that useful in practice. The numbers grow very quickly, and most problems that include the factorial function can (and should) be computed in more efficient ways.

A trivial example is computing binomial coefficients. While it is possible to define them as

choose n k = factorial n `div` (factorial k * factorial (n-k))

it is much more efficient not to use factorials:

choose n 0 = 1
choose 0 k = 0
choose n k = choose (n-1) (k-1) * n `div` k 

So, no, it's not included in the standard prelude. Neither is the Fibonacci sequence, the Ackermann function, or many other functions that while theoretically interesting are not used commonly enough in practice to warrant a spot in the standard libraries.

That being said, there are many math libraries available on Hackage.


The best implementation of factorial I know of in Hackage is Math.Combinatorics.Exact.Factorial.factorial in the exact-combinatorics package. It uses an asymptotically faster algorithm than product [1..n].

http://hackage.haskell.org/package/exact-combinatorics