Logo

Programming-Idioms

History of Idiom 31 > diff from v32 to v33

Edit summary for version 33 by :
New Lua implementation by user [Nepta]

Version 32

2016-02-19, 15:33:27

Version 33

2016-04-07, 06:53:49

Idiom #31 Recursive factorial (simple)

Create recursive function f which returns the factorial of non-negative integer i, calculated from f(i-1)

Idiom #31 Recursive factorial (simple)

Create recursive function f which returns the factorial of non-negative integer i, calculated from f(i-1)

Code
function f(n)
   local function f_(factorial,n)
      if n < 2 then return 1
      return f_(factorial*n, n-1)
   end
   return f_(1,n)
end
Comments bubble
proper tail recursion
Doc URL
http://lua-users.org/wiki/ProperTailRecursion
Demo URL
http://codepad.org/HlGajPjE