c++ vs lisp -- D perspective
bearophile
bearophileHUGS at lycos.com
Wed May 5 02:19:17 PDT 2010
Graham Fawcett:
> I just read a provocative critique of a blog article comparing C++ to
> Lisp:
> http://funcall.blogspot.com/2010/05/c-vs-lisp.html
Three years ago I have written this, for D1 with Phobos that doesn't use my dlibs1:
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);
}
I think it's good enough compared to the Lisp version. I think it's better on the version of this program that was in the Digitalmars site.
Bye,
bearophile
More information about the Digitalmars-d
mailing list