3 ms·
> I would like to add that recursive calls can be nicer than loops Having a good library of higher-order functions that encapsulate common recursion patterns i
by derleth 13y ago
> I would like to add that recursive calls can be nicer than loops
Having a good library of higher-order functions that encapsulate common recursion patterns is a lot nicer than doing recursion by hand, or having to read code where someone did recursion by hand and you have to not only figure out how the recursion works, but whether the author did anything wrong.
Higher-order functions are control structures, and we should encourage their use for all the same reasons we encourage the use of while loops in preference to people building their loop structures by hand out of gotos and conditional gotos.
- eru 13y agoAs another nice effect, higher-order functions (or as we call them in their use as control structures: combinators) unify data structure and control structures. E.g. folding over a list is equivalent to various loops, the Monad instance of Maybe represents simple exception handling, trees and their traversal are identifiable in some sense, etc. At least in lazy, statically typed functional languages, like e.g. Haskell.
- Paradigma11 13y agoI agree but using mutual recursion to model simple state machines is very nice.