Information générale
Catégorie:
Codage, syntaxe et commandes
>What 'types' of programs do you recommend using recursion? I've used them for multi-level types of organizations and relationships where they seem to do well. I'm curious as to other types of applications as well.
Here's the rule of thumb given by one of my professors:
You assume that:
1. You have a little devil-slave that knows how to calculate MyProblem(x)
and
2. You know how to go from y to x
and
3. You know when to stop
Then it's easy to apply a recursive solution:
func MyProblem
lparam y
if MyStopCondition(y)
return whatever && This is my precious knowledge
else
x = NextStepParam(y) && Again, my knowledge
a = MyProblem(x) && Again, my knowledge
return Operate(a, y) && Again, my knowledge
endif
And, as it can be seen, there's no little devil involved at all... :) The devil-slave is just the above pattern. In practice,
there are three important things: the above pattern, the stop condition (very important to be sure that it will
be meet for all cases) and the formula to go from one step to the other (params and result).
Obviously, NextStepParam and Operate can be simple formulas.
Vlad
Précédent
Suivant
Répondre
Voir le fil de ce thread
Voir le fil de ce thread à partir de ce message seulement
Voir tous les messages de ce thread
Voir tous les messages de ce thread à partir de ce message seulement