Pure functions in D

bearophile bearophileHUGS at lycos.com
Thu Oct 2 16:37:40 PDT 2008


> renoX:
> > Sure, D should have monads as soon as Haskell programmers are able to 
> > explain this concept to 'normal' programmers (and no cheating such as 
> > the classical handwaving, this is a way to wrap side effects..)
> > So probably never then.

You can also read this, it's quite easy:
http://www.reddit.com/r/programming/comments/64th1/monads_in_python_in_production_code_you_can_and/c02u9mb

It also shows how they have already solved the problem of exceptions in languages with pure functions, with the Maybe monad :-)

As I've said in the past, functional programming doesn't work alone, it works in an ecology of features. So adding a single thing isn't enough. For example you may need a compiler able of tail call elimination (GCC is able to do this sometimes), have a short lambda syntax (like the C# one: x => x*2 ), have more type inferencing (a stronger type system), closures, simple ways to manage immutability, pure functions, plus other smaller things.

Among the smaller things I'd like there's a generic len() function similar to the Python one and the one I've put in my libs. It's able to call the length attribute if present, and to count items in a lazy iterable too.
You can use it this way:
auto b = sorted(a, &len!(BaseType1!(typeof(a))));
That works, and sorts the items of the a iterable according to the length of its items. BaseType1 is a template that returns the type of the items at the first level that the given type contains/yields.
That shows the type system isn't strong enough yet for higher order functional programming, because len is a function template, and you have to specialize it manually to find its pointer/delegate. In a language with a more powerful type system you may use:
auto b = sorted(a, @len)

Bye,
bearophile



More information about the Digitalmars-d mailing list