Recursion is an integral part of functional programming. The emphasis on recursive methods in functional programming is mainly due to the reason that you don't need a mutable state, hence making it simple and straightforward to semantically define a term. Due to this prevalence of recursion as a functional construct and its semantic differences from other functional programming languages like Haskell, F# provides a keyword for recursion that is, rec
. This is how you define a recursive function:
let rec recursive-function-identifier parameter-list = recursive-function-body
Factorial is usually a simple example to begin explaining how recursion works. To jog your memory, the factorial of n is the product of all the numbers from , that is,
Hence the output will be as follows:
Since F# is a multi-paradigm language, let's first try to solve this using an imperative approach as seen in the next screenshot: