www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Support for functional programming paradigm

reply Glen Perkins <please dontEmail.com> writes:
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++?
Jan 02 2007
next sibling parent Don Clugston <dac nospam.com.au> writes:
Glen Perkins wrote:
 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.
 What support does D offer with respect to inlining and tail recursion?
I think it's quite poor at the moment (in fact, in DMD, I don't think anything with a loop gets inlined :-( ). Essentially, it performs the same optimisations that the back-end (shared with the C++ compilers), there are no D-specific optimisations at present. But Walter's made very clear statements that the situation will improve. The D language enables much much more aggressive optimisation than is possible in similar languages. Post-1.0, I think Walter will start to put more emphasis on performance.
 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++?
* Inner functions. (This actually opens up a whole programming style which isn't possible in those other languages). * Delegates. * Anonymous delegates. * The template metaprogramming system forms a far purer functional programming language than C++'s templates do. There's probably more, but all those features are highly significant.
Jan 02 2007
prev sibling next sibling parent "Andrey Khropov" <andkhropov_nosp m_mtu-net.ru> writes:
Glen Perkins wrote:

 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.
Have you looked at Scala and Nemerle (these are OOP/FP hybrids for .NET and JVM respectively)? -- AKhropov
Jan 02 2007
prev sibling parent reply "Andrey Khropov" <andkhropov_nosp m_mtu-net.ru> writes:
Glen Perkins wrote:

 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.
Have you looked at Scala and Nemerle (these are OOP/FP hybrids for JVM and .NET respectively)? -- AKhropov
Jan 02 2007
parent reply Glen Perkins <please dontEmail.com> writes:
I was aware of Nemerle but not Scala. Thanks for the pointers to both.

There are lots of great ideas in all the nearly unused languages out there. Too
bad developing your own language is such a herculean task that getting what you
really want is almost impossible. You either devote your life to it (think of
Walter, Guido, Matz, Bjarne, etc.) and get what you want, or you find someone
else
who's already devoting HIS life to something similar to what you want and try to
persuade him to modify it. The latter is...well, you know. (And you can hardly
blame them. You devote your life to creating something, usually without pay, and
the least you deserve is a language that's exactly what YOU want.)

I hope Walter spends time playing with non-C-like languages and decides he wants
more of that power in HIS language. He may already be doing exactly that.
Jan 02 2007
next sibling parent Waldemar <waldemar wa-ba.com> writes:
== Quote from Glen Perkins (please dontEmail.com)'s article
 I was aware of Nemerle but not Scala. Thanks for the pointers to both.
 There are lots of great ideas in all the nearly unused languages out there. Too
There is also the D mixin which may be the closest to what you describe.
 bad developing your own language is such a herculean task that getting what you
 really want is almost impossible. You either devote your life to it (think of
 Walter, Guido, Matz, Bjarne, etc.) and get what you want, or you find someone
else
 who's already devoting HIS life to something similar to what you want and try
to
 persuade him to modify it. The latter is...well, you know. (And you can hardly
 blame them. You devote your life to creating something, usually without pay,
and
 the least you deserve is a language that's exactly what YOU want.)
 I hope Walter spends time playing with non-C-like languages and decides he
wants
 more of that power in HIS language. He may already be doing exactly that.
Jan 02 2007
prev sibling parent "Andrey Khropov" <andkhropov_nosp m_mtu-net.ru> writes:
Glen Perkins wrote:

 I was aware of Nemerle but not Scala. Thanks for the pointers to both.
 
 There are lots of great ideas in all the nearly unused languages out there.
In many cases they are unused because of immaturity of compiler/interpreter or libraries. The good thing about Nemerle and Scala is that they are .NET and JVM-based. That means mature VM, solid libraries and transparent interop with other .NET/JVM languages are readily available.
 Too bad developing your own language is such a herculean task that getting
 what you really want is almost impossible. You either devote your life to it
 (think of Walter, Guido, Matz, Bjarne, etc.) and get what you want, or you
 find someone else who's already devoting HIS life to something similar to
 what you want and try to persuade him to modify it.
I think that at least the partial solution to this problem is to allow powerful syntax extensions so you can adapt the language for your own needs. Lisp and Nemerle are the two examples of this approach. -- AKhropov
Jan 02 2007