Factorial in Fortress

Simple recursive function:

factorial(n: ZZ): NN = if n = 0 then 1 else n factorial(n-1) end

Iterative function [[ this would be a great candidate for the use of contracts to capture the invariant of factIter ]]

factorial(n: NN) = do
  factIter(k, kfact) = if k = n then kfact else factIter(k+1, kfact (k+1)) end
  factIter(0, 1)
end

These functions can also be associated with the operator symbol !, as in mathematics:

opr (n: ZZ)! requires { n >= 0 } =
  if n = 0 then 1 else n (n-1)! end

The simplest definition uses a generator

opr (n: ZZ)! requires { n >= 0 } = PRODUCT[k<-1:n] k