Recursive vs RE
Recursive vs RE Recursive A function is recursive if it calls itself. A recursive function can be defined in terms of itself, meaning its output...
Recursive vs RE Recursive A function is recursive if it calls itself. A recursive function can be defined in terms of itself, meaning its output...
Recursive vs RE
Recursive
A function is recursive if it calls itself.
A recursive function can be defined in terms of itself, meaning its output depends on its input.
For example, the factorial function is recursive because it calls itself to calculate the factorial of a number.
RE
A function is RE if it can be expressed as a regular expression.
A RE is a sequence of characters that describes a specific pattern of symbols.
REs can be used to define functions, but they cannot be used to express other functions.
Comparison
| Feature | Recursive | RE |
|---|---|---|
| Definition | Recursive function | Regular expression |
| Can be defined in terms of itself | Yes | Yes |
| Can be used to express other functions | Yes | No |
Examples
Recursive:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
RE:
r = (a|b)*
Key Differences
Recursion allows a function to call itself directly, while REs describe patterns of symbols that can be matched by a regular expression.
Recursive functions can be expressed using themselves, while REs cannot.
REs can be used to define functions, but they are not used to express other functions