Recursion consists in calling a function in the body of this same function. It provides a powerful and elegant way to solve certain kinds of problems.
A typical example is the mathematical function factorial (also noted with !):
5! = 5 x 4 x 3 x 2 x 1
.
The function factorial can be defined as:
factorial(n) - n x factorial(n-1)
.
It is important however, that the recursion have a termination condition. In the case of factorial, we need to complement the definition with:
factorial(0) = 1
In Common Lisp, factorial can be implemented as follows:(defun factorial (n) (if (<= n 0) 1 (* n (factorial (- n 1)))))
Note: Recursion is also a useful and powerful means to handle recursive data structures like trees.
In OM#, visual programs can be recursive as well, as long as they are set as global abstractions. Global (or “external”) abstractions are stored on the disk and referenced in the document manager, and therefore can be used from anywhere (not only locally in the patch where they have been placed — this is the case for “local”, or internal abstractions), including inside themselves.
→ Insert a global abstraction in itself as you would insert another global abstraction, that is, using one of the following options:
- Using the menu “Boxes / Add box… / External abstraction”, then selecting the current file on the disk.
- Using the p shortcut and typing the name of the current patch
- Dragging the current patch file icon from the Finder / file explorer.
See more on “how to use global/external abstractions”.