just brew it! wrote:We've come a long way since MS-DOS .BAT files!
Some of us are still stuck in that world
Personal computing discussed
Moderators: renee, SecretSquirrel, just brew it!
just brew it! wrote:We've come a long way since MS-DOS .BAT files!
derFunkenstein wrote:We went about it a little differently. We were instructed to address base cases first and then do any calculations and finally call the function again. Not that he said it couldn't be done differently, just that dealing with the base cases first will help us keep the code organized.
...
So it'll become automatic over time, I'm sure.
notfred wrote:I've used recursion in walking paths across networks for diagnostics. You may have tunnels and you need to know which nodes the path traverses, including all the nodes the tunnels traverse.
I've also seen recursion bite badly, it led to a stack smash that caused a 10G line card to reboot while running live traffic between two cities.
lycium wrote:In the far far ago, TR used to use the mandelbrot set as a benchmark.I develop a commercial fractal art application, Chaotica, and often get emails asking me how much psychedelic drugs I take... I've never actually tried any (unfortunately?), however to me it's very interesting that people ask all the time - there's clearly something about the human brain that sees fractals (the basis of which is recursion) in altered states.
Of course, one sees fractals very often in nature (trees, clouds, coastlines, ...), and a simple recursive cellular automaton even appears on some sea shells: https://en.wikipedia.org/wiki/Rule_30
SecretSquirrel wrote:This is a very important point. If the problem space doesn't inherently limit the depth of recursion, the algo must have an exit case that prevents the recursion from getting too deep. --SS
if ( dives > 100000 )
return ERR_we_dun_effed_up;
morphine wrote:Code: Select allif ( dives > 100000 )
return ERR_we_dun_effed_up;
public async Task<LoginSettings> GetLoginSettings(retries = 0)
{
var loginSettings = new LoginSettings();
if (retries <= 5)
{
loginSettings = DoSomething();
if (string.IsNullOrEmpty(loginSettings.someRequiredField))
{
await Task.Delay(5000);
return await GetLoginSettings(retries + 1);
}
}
return loginSettings;
}
lycium wrote:Quicksort comes to mind.Speaking in even greater generality, most people employ a recursive algorithm in general problem solving: "divide and conquer"....
Flying Fox wrote:You are just doing retries. Could have just rewrite this without the "recusive" call. You are already doing the async pattern which can be tricky to begin with (no CancellationToken?). Besides, your DoSomething is sync, I am not sure why you want to make this async.
Ninja: what's your error condition if retries are exhausted? A "blank" object? Perhaps null would be better? Or an exception?
if (loginSettings == null)
{
await CrapYourPantsInAGracefulWay();
}