3 ms·
I would like to add that recursive calls can be nicer than loops, when multiple conditions or branching are possible in the loop body. An equivalent implementa
by a-nikolaev 13y ago
I would like to add that recursive calls can be nicer than loops, when multiple conditions or branching are possible in the loop body.
An equivalent implementation with while loops, if conditions, continues, and breaks are usually harder to design correctly than a good recursive function.
Functional programming encourages use of small simple datatypes. Tuples instead of small structs. Lists instead of more complex container types. First-class functions instead of classes.
However, IMO 'purity' is a bit of a buzzword when applied to functional programming. You may disagree, but that's how I feel about it..
- Peaker 13y agoI think recursive solutions are often very nice indeed. But at worst, recursive solutions are sometimes worse than structured loops, because you can use recursions very much like a free-style goto, allowing your code to have even less restrictive structure than loops. That's why directly recursive code is usually discouraged and reusing recursive combinators is encouraged. I agree that purity is often used as a buzzword. To parody this, Conal Elliott wrote the "C is purely functional" blog post: http://conal.net/blog/posts/the-c-language-is-purely-functional http://conal.net/blog/posts/the-c-language-is-purely-functio... To resolve this, a nice suggestion is to use the term "denotative programming". That is, programming where types and values have mathematically simple denotations that predictably compose exactly according to the syntactic composition being used.
- a-nikolaev 13y agoThanks for the idea of "denotative programming". I think, it can be a really nice paradigm for teaching programming and math together.
- ufo 13y agoTail-recursive calls are essentially gotos, but a little more well behaved (if you try to put state into explicit parameters instead of mutable variables).
- 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.