Logo

Programming-Idioms

  • Erlang
  • Groovy

Idiom #31 Recursive factorial (simple)

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

def f(i) { i == 0 ? 1 : i * f(i - 1) }
f(0) -> 1;
f(I) -> I * f(I - 1).

No "defensive programming" here.
function F (I : Natural) return Natural is (if I < 2 then 1 else I * F (I - 1));

New implementation...
< >
programming-idioms.org