Walter Bright:
> Also, if you're only writing a few K of code, D's advantages aren't that
> compelling over C (and neither are C++'s). It's when the size of the
> program increases that D's strengths really begin to dominate.
I don't agree at all. D is (and has to be) fitter for short programs too, that
means that even in short programs it (has to) show(s) its advantages. This is
one of the first programs I've written in D, a lot of time ago, to solve a
problem that's already present on your site (but there the solution is much
less nice), this program is very short, but if you try to use C to write the
same code you will produce lot of more code, several bugs, and a headache:
import std.stdio, std.stream, std.string, std.ctype, std.gc;
void traduct(char[] n, char[] digits, int start, char[][] words,
char[][][char[]] gdict) {
if (start >= digits.length)
writefln(n, ": ", words.join(" "));
else {
auto found_word = false;
for(auto i = start; i < digits.length; i++)
if (digits[start .. i+1] in gdict) {
found_word = true;
foreach(hit; gdict[digits[start .. i+1]])
traduct(n, digits, i+1, words ~ [hit], gdict);
}
if (!found_word && (!words || (words &&
!std.ctype.isdigit(words[words.length-1][0]))))
traduct(n, digits, start+1, words ~ [digits[start..start+1]],
gdict);
}
}
void main() {
std.gc.disable(); // to speed up the program a bit
auto gtable =
maketrans("ejnqrwxdsyftamcivbkulopghzEJNQRWXDSYFTAMCIVBKULOPGHZ",
"0111222333445566677788899901112223334455666777888999");
char[][][char[]] gdict;
foreach(char[] w; new BufferedFile("dictionary.txt"))
gdict[w.translate(gtable, "\"")] ~= w.dup;
foreach(char[] n; new BufferedFile("input.txt"))
traduct(n, n.removechars("/-"), 0, [], gdict);
}
Bye,
bearophile