Recursion

In functional programming, looping is performed by recursive functions. In F#, to make a function recursive, we need to use the rec keyword. By default, functions are not recursive in F#, we have to rectify this explicitly using the rec keyword. Let's take an example:

let rec summation x = if x = 0 then 0 else x + summation(x-1)printfn "The summation of first 10 integers is- %A" (summation 10)

In this code, we used the keyword rec for the recursion function and if the value passed is 0 , the sum would be 0; otherewise it will add x + summation(x-1), like 1+0 then 2+1 and so on. We should take care with recursion because it can consume memory heavily. 

Get .NET Core 2.0 By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.