More compact code is rarely good in my experience. You can make perl 1 liners that do amazing things, but even perl experts need to spend minutes decoding it. Given a choice between 5 lines that are easy to understand, and one line that does it elegantly but is hard to understand, I'll take 5. Hell, I'll take 50, plop them into a function, and give the function a nice name. For example:From: Tracy R Reed <[EMAIL PROTECTED]> I think the idea of functional programming is very interesting and I am puzzled as to why it has not grown in popularity. More compact code meaning less to verify/debug,
while((*x++)++);
or
while(x){
(*x)++;
x++;
}or
i=0;
while(x[i]!=0){
x[i]++;
i++;
}(Note: I think the first while loop is equivalent, it may have an off by 1 error. I'm too lazy to check. And I'm trying to get something done at work. And I'd shoot anyone who actually used it in code. So sue me.).
All 3 add 1 to every variable in a 0 terminated array. The first is the most compact, and least understandable. The second is better, but still requires you to think through pointer incrementation. The third is best- very clear on exactly what its doing, and uses the more verbose but clearer array notation.
An added bonus is that 2 and 3 are easier to modify and debug.
no side-effects
Then don't write side effects. They're easily avoided. Side effects in C occur for 3 reasons.
1)Using ++ or --. Don't unless they are on their own line.
2)Using macros for functions. Don't, without damn good reasons. C99 included inline functions.
3)Usage of global variables. I won't say don't here, because global variables very frequently make things easy. Just be aware. I believe this can also occur in Lisp. If it can't, then that means lisp has no global variables, which would be a huge problem in my mind- globals make life easy, frequently.
, inherent modularity, etc.
C has inherent modularity. Use and love the static and extern keywords.
The only reason I can think of for the lack of popularity is that LISP got a reputation for being slow back when cpu time was expensive and C and other imperative languages took over all of the mindshare.
It due to thought processes. C is how computers think. Exactly how they think. One instruction followed by another. Its also how a lot (most) of engineers think. Including myself. IMO, the closer you are to how the computer thinks, the easier it is to write and debug. Lisp, with its stack based and recursion based processes, isn't as simple and elegant as sequential thought. It might be how some top mathematicians think, but lets be honest very few people are top rank mathematicians. I've taught college math and I don't rank as one.
I see what you want done as a series of steps saying how you want it done. Inserting a fictional distinction between the two complicates the problem. If I asked you how to get to the airport, would you describe the airport parking lot, or tell me to take the 15 to the 163 to the 5?
I love the idea of functional programming as being described as what you want done not how you want it done. In learning about cfengine I see that cfengine employs similar ideas in that you describe the idea system state rather than how to get there.
Not that learning it is a bad idea. I learned ML in college and I wasn't harmed by the experience. At the very least it nailed down recursion into my mind. But languages really seem to have hit their peak with C. Other languages have added some syntactic sugar onto it, but there's little to nothing that they do that C doesn't as or more elegantly. Classes and templates are nice, but have as many drawbacks leading to ugly code as they do positives.
Gabe
--
KPLUG-List mailing list [email protected] http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-list
