Yes, there are many times, I would not use recursion. Recursion is not free, it has costs in the stack space and can often be a much more limited resource than some others. There is also a temporary cost, albeit small, when setting up and breaking stack frames.
As an example, a highly praised factorial function is one where I would probably prefer an iterative approach when numbers were large. Computing 10000! with:
def factorial (n):
if n = 1 return 1
return n * factorial (n-1)
will use 10000 stack frames (provided that it is not optimized by the compiler in an iterative solution, of course), quite a lot. Iterative solution:
def factorial (n)
r = 1
while n > 1:
r = r * n
n = n - 1
return r
will use only one stack frame and some more precious.
, , .
carbon - , , :
, Python :
def recur (str, pref = ""):
if str == "":
print pref
return
for i in range (len (str)):
recur (str[1:], pref + str[:1])
str = str[1:] + str[:1]
recur ("abc")
:
abc
acb
bca
bac
cab
cba
, 10K, , , , , .