Could I ask for physical analogies or metaphors for recursion?

后端 未结 5 892
时光取名叫无心
时光取名叫无心 2020-12-18 03:22

I am suddenly in a recursive language class (sml) and recursion is not yet physically sensible for me. I\'m thinking about the way a floor of square tiles is sometimes a mo

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-18 03:55

    I love this question and couldn't resist to add an answer...

    Recursion is the russian doll of programming. The first example that come to my mind is closer to an example of mutual recursion :

    Mutual recursion everyday example

    Mutual recursion is a particular case of recursion (but sometimes it's easier to understand from a particular case than from a generic one) when we have two function A and B defined like A calls B and B calls A. You can experiment this very easily using a webcam (it also works with 2 mirrors):

    1. display the webcam output on your screen with VLC, or any software that can do it.
    2. Point your webcam to the screen.
    3. The screen will progressively display an infinite "vortex" of screen.

    What happens ?

    • The webcam (A) capture the screen (B)
    • The screen display the image captured by the webcam (the screen itself).
    • The webcam capture the screen with a screen displayed on it.
    • The screen display that image (now there are two screens displayed)
    • And so on.

    You finally end up with such an image (yes, my webcam is total crap):

    "Simple" recursion is more or less the same except that there is only one actor (function) that calls itself (A calls A)

    "Simple" Recursion

    That's more or less the same answer as @WillNess but with a little code and some interactivity (using the js snippets of SO)

    Let's say you are a very motivated gold-miner looking for gold, with a very tiny mine, so tiny that you can only look for gold vertically. And so you dig, and you check for gold. If you find some, you don't have to dig anymore, just take the gold and go. But if you don't, that means you have to dig deeper. So there are only two things that can stop you:

    1. Finding some gold nugget.
    2. The Earth's boiling kernel of melted iron.

    So if you want to write this programmatically -using recursion-, that could be something like this :

    // This function only generates a probability of 1/10
    function checkForGold() {
      let rnd = Math.round(Math.random() * 10);
      return rnd === 1;
    }
    
    function digUntilYouFind() {
      if (checkForGold()) {
        return 1; // he found something, no need to dig deeper
      }
      // gold not found, digging deeper
      return digUntilYouFind();
    }
    
    let gold = digUntilYouFind();
    console.log(`${gold} nugget found`);
    

    Or with a little more interactivity :

    // This function only generates a probability of 1/10
    function checkForGold() {
      console.log("checking...");
      let rnd = Math.round(Math.random() * 10);
      return rnd === 1;
    }
    
    function digUntilYouFind() {
      if (checkForGold()) {
        console.log("OMG, I found something !")
        return 1;
      }
      try {
        console.log("digging...");
        return digUntilYouFind();
      } finally {
        console.log("climbing back...");
      }
    }
    
    let gold = digUntilYouFind();
    console.log(`${gold} nugget found`);

    If we don't find some gold, the digUntilYouFind function calls itself. When the miner "climbs back" from his mine it's actually the deepest child call to the function returning the gold nugget through all its parents (the call stack) until the value can be assigned to the gold variable.

    Here the probability is high enough to avoid the miner to dig to the earth kernel. The earth kernel is to the miner what the stack size is to a program. When the miner comes to the kernel he dies in terrible pain, when the program exceed the stack size (causes a stack overflow), it crashes.

    There are optimization that can be made by the compiler/interpreter to allow infinite level of recursion like tail-call optimization.

提交回复
热议问题