“Necessary” Uses of Recursion in Imperative Languages

后端 未结 9 1054
独厮守ぢ
独厮守ぢ 2020-12-17 17:10

I\'ve recently seen in a couple of different places comments along the lines of, \"I learned about recursion in school, but have never used it or felt the need for it since

9条回答
  •  -上瘾入骨i
    2020-12-17 17:55

    It's all about the data you are processing.

    I wrote a simple parser to convert a string into a data structure, it's probably the only example in 5 years' work in Java, but I think it was the right way to do it.

    The string looked like this:

    "{ index = 1, ID = ['A', 'B', 'C'], data = {" +
       "count = 112, flags = FLAG_1 | FLAG_2 }}"
    

    The best abstraction for this was a tree, where all leaf nodes are primitive data types, and branches could be arrays or objects. This is the typical recursive problem, a non-recursive solution is possible but much more complex.

提交回复
热议问题