Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive functions in F#

Tags:

recursion

f#

I am not sure if this is the right place to ask this kind of question, so please don't hate. I recently started learning F# for fun on edx.org. We had a lesson about recursion. The author of that course gave an interesting example:

let rec T a =
    if a = 0 then
        2
    else
        T (a - 1) * a 

He then set a to be 4 and the result output was 48. I have been thinking about this one for some time now and I don't get it. Since 4 isn't equal to 0, we then get T (4 - 1) * 4, which is 12. Since a keeps decreasing by one every turn, which position takes the result 12? Is it T (3 - 1) * 12 or something else? How do we get the result 48?

like image 790
Milan Todorovic Avatar asked Feb 19 '26 19:02

Milan Todorovic


1 Answers

T(4) = T(3)*4 = (T(2)*3)*4 = ((T(1)*2)*3)*4 = (((T(0)*1)*2)*3)*4) = 2*1*2*3*4 = 48

like image 119
Scott Hunter Avatar answered Feb 21 '26 15:02

Scott Hunter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!