Support for functional programming paradigm

Glen Perkins please at dontEmail.com
Tue Jan 2 01:23:51 PST 2007


My coding style seems to take on more functional flavor with each passing
year. Especially when I don't know what I'm doing, meaning I'm doing something
interesting, I like to build up from small functions, use a lot of recursion, etc.

Two things that would help me a lot in any language would be serious inlining
support and unwinding of tail recursion. The former would make the generated
code from a composite of small functions just as efficient as if I had
manually dumped all of the logic into a single function. The latter unwinds
tail recursion so that the object code doesn't actually make the function
calls over and over. As long as recursion is tail recursive, meaning calling
itself only happens as the last step in the function, it can always be unwound
into an equivalent iterative process (loop). Scheme, for example, requires in
the spec that tail recursion be unwound by the interpreter/compiler. Python,
unfortunately, takes the opposite approach. Guido (the Walter Bright of
Python) told me at a user group meeting that average programmers don't
understand things like tail recursion, so all he intended to do about it was
to enable programmers who were afraid they might overflow their stack to
manually increase the size of the stack! Nothing more would be done. He said
that the few programmers who were smart enough to understand tail recursion
would be smart enough to manually unwind their recursive algorithms into
iterative ones. (Of course they could, but that's the machine's job, not the
programmer's. Though I like Python and use it, Guido's "design for what
average joes understand" ethic makes it less than I would hope at times.)

What support does D offer with respect to inlining and tail recursion?

For those of you who are functional programmers yourselves (I am, but it's not
the only paradigm I use, and I'm hardly a purist about it when I use it), what
features does D have (if any) that might enable a more functional style of
programming than you can get from, say, Java or C++?



More information about the Digitalmars-d mailing list