Go rant
Daniel de Kok
me at danieldk.eu
Mon Dec 21 09:54:19 PST 2009
On 2009-12-19 21:04:32 +0100, Andrei Alexandrescu
<SeeWebsiteForEmail at erdani.org> said:
> "This code is shown for its elegance rather than its efficiency. Using
> ++ in this way is not generally considered good programming practice."
>
> So if the code is inefficient and in poor programming practice, how in
> this blessed world could it count as elegant?
Well, it is elegant in that it is very readable, and as such provides
educational value. There are very many examples in programming text
books that may not be efficient, but do show the solution to a problem
elegantly.
E.g. consider list reversal in a recursive function or predicate. It is
easy to see that to reverse a list, you append the head to the reversed
tail. E.g.:
reverse([],[]).
reverse([H|T],R) :-
reverse(T,RT),
append(RT,[H],R).
Of course, this is very inefficient, since the program is doing (slow)
appends all the time, and is not tail-recursive. You can do it a lot
faster with an accumulator, but it makes the solution also less
understandable, and not something you'd want to present
students/readers immediately:
reverse(List,Reverse) :-
reverse_aux(List,Reverse,[]).
reverse_aux([],List,List).
reverse_aux([H|T],List,List0) :-
reverse_aux(T,List,[H|List0]).
> I have gathered a fair amount of samples of involuntary humor from that
> book. I wouldn't want to go on about that because it could too easily
> be interpreted as poor taste competitiveness. Let me say I don't think
> the book is well written.
It seems that currently the time to market is much more important than
to provide good material, although I cannot comment on that particular
book.
-- Daniel
More information about the Digitalmars-d
mailing list