← Back to context

Comment by bjoli

17 hours ago

I am in the other camp. I prefer tail recursion and recursion over loops. However: For the simple cases it can and should probably be abstracted away like the racket for loops or my own goof-loop [1].

I just feel that a recursive calls makes state much more clear, but then again I am no fan of mutation in general. In my old python days I think a good 60% of my bugs were due to me being bad at managing state.

[1] https://rikspucko.koketteriet.se/bjoli/goof-loop

I'm in the same boat, recursion tends to be easier for me to reason about because I'm expressing the problem in terms of some base case that incoming parameters are being reduced to rather than some condition that an iterative approach is working towards.

  • I prefer recursion over loops. But even more I prefer abstracting away the recursion into combinators.

    One of my favourite combinators is matrix multiplication. You define what 'addition' and 'multiplication' mean in your case, and all of a sudden you get an algorithm that computes shortest paths in a graph or does regex matching. See https://news.ycombinator.com/item?id=9751987

    But for more bread and butter cases, there's 'map' over various data structures, and 'reduce' and traversals of trees etc.