“Necessary” Uses of Recursion in Imperative Languages

后端 未结 9 1075
独厮守ぢ
独厮守ぢ 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条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-17 17:58

    In my opinion, recursive algorithms are a natural fit when the data structure is also recursive.

    def traverse(node, function):
        function(this)
        for each childnode in children:
            traverse(childnode, function)
    

    I can't see why I'd want to write that iteratively.

提交回复
热议问题