Personal computing discussed

Moderators: SecretSquirrel, just brew it!

• 1
• 2

SuperSpy
Gold subscriber
Minister of Gerbil Affairs
Posts: 2214
Joined: Thu Sep 12, 2002 9:34 pm
Location: TR Forums

### Re: How recursive is the real world?

just brew it! wrote:
We've come a long way since MS-DOS .BAT files!

Some of us are still stuck in that world
Desktop: i7-4790K @4.8 GHz | 32 GB | EVGA Gefore 1060 | Windows 10 x64
Laptop: i7 740QM | 12 GB | Mobility Radeon 5850 | Windows 10 x64

notfred
Maximum Gerbil
Posts: 4361
Joined: Tue Aug 10, 2004 10:10 am

### Re: How recursive is the real world?

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.

Redocbew
Gold subscriber
Graphmaster Gerbil
Posts: 1232
Joined: Sat Mar 15, 2014 11:44 am

### Re: How recursive is the real world?

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.

Yeah, it does. Recursion helps to give you an appreciation for the complexity of certain problems even if you don't use it much. Once you learn it, recursion makes everything easier.

Making the recursive call at the end of the function is called tail recursion, which 223 Fan mentioned earlier. Being able to use tail recursion usually means an iterative solution wouldn't be too difficult, but it does help to keep things simple. There are some cases, like in an XML parser which has to walk the document tree and perform some computation on each node afterwards where tail recursion may not be suitable, and that's when things can start to get interesting.
Do not meddle in the affairs of archers, for they are subtle and you won't hear them coming.

SecretSquirrel
Minister of Gerbil Affairs
Posts: 2276
Joined: Tue Jan 01, 2002 7:00 pm
Location: The Colony, TX (Dallas suburb)
Contact:

### Re: How recursive is the real world?

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.

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

Mr Bill
Gold subscriber
Graphmaster Gerbil
Posts: 1421
Joined: Mon Jan 21, 2002 7:00 pm
Contact:

### Re: How recursive is the real world?

lycium wrote:
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
In the far far ago, TR used to use the mandelbrot set as a benchmark.
X6 1100T BE | Gigabyte GA-990FXA-UD3 AM3+ | XFX HD 7870 | 16 GB DDR3 | Samsung 830/850 Pro SSD's | Logitech cherry MX-brown G710+ | Logitech G303 Daedalus Apex mouse | SeaSonic SS-660XP 80+ Pt | BenQ 24' 1900x1200 IPS | APC Back-UPS NS-1350 | WinXP64 Pro

just brew it!
Gold subscriber
Posts: 48973
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

### Re: How recursive is the real world?

FWIW, although the Mandelbrot set has the self-similarity at multiple scales typical of fractals, the usual method for calculating it is iterative, not recursive.

Edit: Obligatory Mandelbrot fractal "deep zooms" (raw frames originally rendered on my FX-8350 desktop, using a custom Python/C program):
http://uchima.net/fractal-videos/mbzoom16.mp4
http://uchima.net/fractal-videos/mbzoom22.mp4
Nostalgia isn't what it used to be.

morphine
Gold subscriber
Gerbilus Supremus
Posts: 11376
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

### Re: How recursive is the real world?

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;``
There is a fixed amount of intelligence on the planet, and the population keeps growing :(

Captain Ned
Gold subscriber
Global Moderator
Posts: 26319
Joined: Wed Jan 16, 2002 7:00 pm
Location: Vermont, USA

### Re: How recursive is the real world?

morphine wrote:
``if ( dives > 100000 )    return ERR_we_dun_effed_up;``

Eddie could have used that when faced with the Tea Question.
If the Earth were flat, cats would have pushed everything off of it by now.

derFunkenstein
Gold subscriber
Gerbil God
Topic Author
Posts: 23624
Joined: Fri Feb 21, 2003 9:13 pm
Location: Comin' to you directly from the Mothership

### Re: How recursive is the real world?

OK, so I just used recursion in the real world.

The QA instances of the web app that my mobile project goes with get rebuilt every morning and maybe a couple times throughout the day. If the mobile app is the first app to access those instances, they have to spin up. The web client tier deals with this by displaying a "loading" message and refreshing the page every second until it gets a valid response. Up until today, the Xamarin Forms re-write (hooray for dumping obscure platforms!) would just display a blank login page and no indication that anything was loading.

So I figured out that if one of the mandatory fields is empty, I could sleep for five seconds and retry, until a total of 30 seconds has passed like so. This isn't rocket science, and I'm sure you seasoned vets will laugh because this is a simple problem, but I'm really quite happy to use something from a class that seemed abstract in a real way.

``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;}``
"And and if you start to bleed, stop wiping." -whm1974

Flying Fox
Gerbil God
Posts: 25408
Joined: Mon May 24, 2004 2:19 am
Contact:

### Re: How recursive is the real world?

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?
The Model M is not for the faint of heart. You either like them or hate them.

Gerbils unite! Fold for UnitedGerbilNation, team 2630.

Mr Bill
Gold subscriber
Graphmaster Gerbil
Posts: 1421
Joined: Mon Jan 21, 2002 7:00 pm
Contact:

### Re: How recursive is the real world?

lycium wrote:
Speaking in even greater generality, most people employ a recursive algorithm in general problem solving: "divide and conquer"....
Quicksort comes to mind.
X6 1100T BE | Gigabyte GA-990FXA-UD3 AM3+ | XFX HD 7870 | 16 GB DDR3 | Samsung 830/850 Pro SSD's | Logitech cherry MX-brown G710+ | Logitech G303 Daedalus Apex mouse | SeaSonic SS-660XP 80+ Pt | BenQ 24' 1900x1200 IPS | APC Back-UPS NS-1350 | WinXP64 Pro

derFunkenstein
Gold subscriber
Gerbil God
Topic Author
Posts: 23624
Joined: Fri Feb 21, 2003 9:13 pm
Location: Comin' to you directly from the Mothership

### Re: How recursive is the real world?

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?

Error is handled in the ViewModel when the "blank" object is returned. Give the user an error that the server isn't responding and redirect them to the instance lookup page, because closing out entirely when a server may have just moved seemed kinda rude.

``if (loginSettings == null){   await CrapYourPantsInAGracefulWay();}``

Also there's about 6 lines of code represented by DoSomething from initializing a WebRequest to retrieving it to deserializing a JSON object. It wasn't the point of my example, so I just put a placeholder. It's all running asynchronously, don't worry.

You can do a lot of stuff without recursion. This could have been in a do (while) loop but I spotted a use for something that didn't seem like too big of a stretch so I used it. I don't expect anybody here to be impressed with my coding skillz.
"And and if you start to bleed, stop wiping." -whm1974

Redocbew
Gold subscriber
Graphmaster Gerbil
Posts: 1232
Joined: Sat Mar 15, 2014 11:44 am

### Re: How recursive is the real world?

I approve of any attempt for code to crap its pants gracefully. There's an alarming amount of code out there which is simply incontinent by comparison.
Do not meddle in the affairs of archers, for they are subtle and you won't hear them coming.

notfred
Maximum Gerbil
Posts: 4361
Joined: Tue Aug 10, 2004 10:10 am