• Blackmist@lemmy.world
    link
    fedilink
    English
    arrow-up
    13
    ·
    1 year ago

    When navigating a tree structure.

    Processing files and folders, expression parsing, that kind of thing.

    I’ve no idea why the factorial example is so popular, because it’s one of the worst use cases for it. Still, I guess it can teach a new programmer what a stack overflow is.

    • Faresh@lemmy.ml
      link
      fedilink
      English
      arrow-up
      4
      ·
      1 year ago

      I’ve no idea why the factorial example is so popular,

      It is because anyone who has done anything with math (as is the case with cs students), will know how the factorial is defined in math which is also a recursive definition, so the code ends up looking almost exactly like the definition in math, and because of its simplicity, it’s excellent to introduce students to the idea of recursion. So, it’s a combination of familiarity and simplicity that makes it such a well known example.

      Of course, in the real world you would use the implementation from a library. So whether recursion or iteration is better for factorials isn’t that important of a question, since you won’t be using your solution in a normal situation. The factorial example is supposed to be only used as nothing more than an example.