Type Kung-Fu
Type safety, or more simply put, the reason ML use leads to fewer errors than C, is cool. Yes I know C is cool, and close to assembly and all of that good stuff, I probably wrote as many, if not more lines of C code than the next person over the course of the last year, I like systems programming and all that fine stuff, and I spent most of last night (let us just say from about 7 pm to 6 am) staring at C code, and I know that some, who’s kung fu is significantly older than mine, created this entire operating system by flipping bits, and while hacking through raw stuff is fun and all, strong typing, or compilers which give intelligent warnings by default (rather than weird void* to pointer cast warnings) would be much appreciated. In general, type safety would be much appreciated, see piece of code below, it would never have compiled in Ocaml, and would not have taken me and a friend seven and a half hours to hunt down, and hence would not have kept us up all night (it really did, I went to sleep at noon yesterday). What follows is a comparator for two structures, which contain cost, weight pairs, so that qsort can sort them in descending order. The problem is sorting is a part of getting data off a file, and is a fairly minor part of the algorithm (Branch and Bound for the knapsack problem). I guess even the most well done of algorithms can be thwarted by tiny errors, either ways, type safety is good.
int cmp_cwstruct(const cost_weight_struct * str1, const cost_weight_struct *str2) {
double cwt1 = (float)str1->cost / (float)str1->weight;
double cwt2 = (float)str2->cost / (float)str2->weight;
return cwt2-cwt1;
}
Ze Panda
Add New Comment
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Add New Comment
Trackbacks