Most programmers learn iteration first, and many find it hard to get their heads around recursion. However, because recursion in Full Metal Jacket is similar to recursion in other programming languages, and iteration is quite different, recursion is explained here first. Recursive functions contain, and rely upon, a conditional expression with several clauses. If certain conditions are satisfied, the function calls itself, but with simpler (i.e. reduced) arguments; otherwise, a value is returned directly.
factorial(n) = if n <= 1 then 1 else n * factorial(n-1) There are several other ways of defining factorial, for example using tail recursion or iteration. In Full Metal Jacket, Incidentally, Common Lisp calls the function to subtract 1
![]() ![]() © Copyright Donald Fisk 2015, 2016 |